gpt4 book ai didi

java - 未找到 HTTP 404

转载 作者:行者123 更新时间:2023-12-02 00:54:53 32 4
gpt4 key购买 nike

我在运行 Spring Boot 应用程序后打开 localhost 时遇到问题。

当我运行 localhost:9091 时,出现与 http 404 not found 相关的问题。

我该如何解决它。

Application.java(包 -> com.springboot.demo)

@SpringBootApplication
@ComponentScan(basePackages="com.springboot.demo")
public class Application extends SpringBootServletInitializer{

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}

}

FunRestController.java(包 -> com.springboot.demo.rest)

@RestController
public class FunRestController {

// expose "/" that return "Hello World"

@GetMapping("/")
public String sayHello() {
return "Hello World! Time on server is " + LocalDateTime.now();
}

}

应用程序属性

server.port = 9091

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.1.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.springboot.demo</groupId>
<artifactId>27_Spring_Boot_MyCoolApp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>27_Spring_Boot_MyCoolApp</name>
<description>Demo project for Spring Boot</description>

<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven-jar-plugin.version>3.1.1</maven-jar-plugin.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>
</dependency>

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

</dependencies>

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

</project>

最佳答案

问题在于依赖关系,替换:

<artifactId>spring-boot-starter</artifactId>

<artifactId>spring-boot-starter-web</artifactId>

删除:

<artifactId>spring-web</artifactId>

<artifactId>spring-boot-starter-tomcat</artifactId>

尝试以下 pom:

<?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.1.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.springboot.demo</groupId>
<artifactId>27_Spring_Boot_MyCoolApp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>27_Spring_Boot_MyCoolApp</name>
<description>Demo project for Spring Boot</description>

<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
</properties>

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

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

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

</project>

输出:

Hello World! Time on server is 2019-09-06T21:41:40.435

关于java - 未找到 HTTP 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57827086/

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