Spring Boot Tomcat无法在Sping Boot 应用程序中启动

nvbavucw  于 2023-10-16  发布在  Spring
关注(0)|答案(1)|浏览(129)

我想创建一个新的Sping Boot 测试项目,其中包含一个返回消息的基本服务,但当我启动服务时,服务运行,但Tomcat服务器不启动,我不明白为什么。
我有Sping Boot 3.1.4版和Java 17版。
这是控制台返回的内容:

:: Spring Boot ::                (v3.1.4)

2023-10-15T16:37:07.111+02:00  INFO 21264 --- [           main] com.example.demo.Demo4Application        : Starting Demo4Application using Java 17.0.8.1 with PID 21264 (C:\Users\Pilou\Documents\Master2\TP MicroService\demo4\target\classes started by Pilou in C:\Users\Pilou\Documents\Master2\TP MicroService\demo4)
2023-10-15T16:37:07.113+02:00  INFO 21264 --- [           main] com.example.demo.Demo4Application        : No active profile set, falling back to 1 default profile: "default"
2023-10-15T16:37:07.469+02:00  INFO 21264 --- [           main] com.example.demo.Demo4Application        : Started Demo4Application in 0.61 seconds (process running for 0.954)

Process finished with exit code 0

我试图通过在pom.xml中这样做来强制使用它:

<dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-tomcat</artifactId>
     </dependency>

然后服务器启动,但它系统地返回一个404错误。
我应该指出的是,我在application.properties中配置了端口,并尝试了几次用Maven清理它。
我的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>3.1.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo4</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo4</name>
    <description>demo4</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>6.0.12</version>
        </dependency>
    </dependencies>

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

</project>
vatpfxk5

vatpfxk51#

请重写pom.xml中的依赖部分:

<dependencies>
    <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

然后在根包中实现ApplicationLuncher类:

@RestController
@SpringBootApplication
public class ApplicationLauncher {
  
  public static void main(String[] args) {
     SpringApplication.run(ApplicationLauncher.class, args);
  }

  @GetMapping
  public String helloWorld() {
    return "Hello World";
  }
}

然后开始 Spring 应用。要通过curl命令获取Hello World,请写入curl http://localhost:8080

相关问题