gpt4 book ai didi

java - 为什么我在运行 Spring Boot 项目时总是收到状态为 “404” 的 Whitelabel 错误页面

转载 作者:行者123 更新时间:2023-12-01 16:15:35 26 4
gpt4 key购买 nike

我的 pom.xml 包含我需要的所有依赖项,例如 Spring Boot 及其 Web 依赖项

<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>a-LABO</groupId>
<artifactId>a-LABO</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>src</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
</parent>

<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>

</project>

我的 Controller 类:

包main.java;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {

@RequestMapping("/hello")
public String SayHi() {
return "Hi!";
}

}

这是我的主(入口点)类,Tomcat 在其中启动

@SpringBootApplication
public class Application {

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

@Bean
public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
return args -> {

System.out.println("Let's inspect the beans provided by Spring Boot:");

String[] beanNames = ctx.getBeanDefinitionNames();
Arrays.sort(beanNames);
for (String beanName : beanNames) {
System.out.println(beanName);
}

};
}

}

我的 UserController 位于 src/controllers 中。现在的问题是,当我导航到/hello 时,我总是收到 spring 错误页面。这是为什么?我该如何解决它?

谢谢

最佳答案

确保 UserController 属于应用程序包的子包,以确保在组件扫描期间扫描 UserController

关于java - 为什么我在运行 Spring Boot 项目时总是收到状态为 “404” 的 Whitelabel 错误页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62398173/

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