gpt4 book ai didi

Java 应用程序在运行 Spring Boot 时可以工作,但不能与 Apache 一起工作

转载 作者:行者123 更新时间:2023-11-30 05:18:36 25 4
gpt4 key购买 nike

我使用 https://start.spring.io/ 创建了一个应用程序因此,当我运行 ./mvnw spring-boot:run 时,应用程序运行良好。但是,为了能够调试我的应用程序(就像我总是对其他应用程序所做的那样),我选择“在服务器上调试”,服务器编译成功,并显示消息INFORMACIÓN:服务器在 4288 毫秒内启动

但是当我尝试访问该应用程序时,出现 404 错误。

这是我的 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.domain.app</groupId>
<artifactId>app</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>app</name>
<description>app</description>

<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-core</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>compile</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

这是我的 Controller :

@RestController
@RequestMapping(
value = "/api/products",
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE
)
public class ProductController {

@Autowired
ProductService productService;

@GetMapping("")
@ResponseStatus(HttpStatus.OK)
public String getAllProducts() {
return this.productService.initCrawler();
}
}

我指向的 URL 是:

http://localhost:8080/app/api/products

响应是HTTP 404

构建后的 Eclipse 在 http://localhost:8080/app 中打开资源管理器,但它也得到 404。

直接使用 ./mvnw spring-boot:run 运行时,正常工作的 URL 是 http://localhost:8080/api/products当我使用作为服务器运行

时,它也不起作用

日志中没有说明任何内容...

最佳答案

我是这样理解你的问题的。您已经从 Spring Initializr 创建了一个 Spring Boot 应用程序,并且该应用程序从命令行成功运行,但当您尝试在服务器中部署并以 Debug模式启动服务器时却无法运行。

您应该知道 Spring Initializr 创建了一个 Spring Boot 应用程序,它是独立的,是您可以“直接运行”的基于 Spring 的生产级应用程序。独立意味着服务器作为 Spring Boot 应用程序的依赖项合并,您不需要部署在服务器中。

如果要部署在服务器中,则需要使 Spring Boot Application @SpringBootApplication 类扩展 SpringBootServletInitializer 类。


@SpringBootApplication
public class DemoApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(DemoApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}

您应该在 Maven 中指定将要运行的主类,并将打包从 jar 更改为 war。

<packaging>war</packaging>
<start-class>com.tutorialspoint.demo.DemoApplication</start-class>

有关如何使 Spring Boot 应用程序部署在服务器中的更多信息,请查看链接:https://www.tutorialspoint.com/spring_boot/spring_boot_tomcat_deployment.htm

如果您不需要应用程序在服务器上运行,您只想调试代码。我建议使用 eclipse 或 intellij。您可以在透视图中包含 Spring Boot 仪表板,并在 Debug模式下启动 Spring Boot 应用程序。检查此链接以了解如何包含和使用 Spring Boot 仪表板:https://medium.com/danielpadua/java-spring-boot-eclipse-744454468670

关于Java 应用程序在运行 Spring Boot 时可以工作,但不能与 Apache 一起工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59924213/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com