为啥我在运行简单的 Spring Boot 应用程序时总是得到状态为“404”的 Whitelabel 错误页面

     2023-02-26     68

关键词:

【中文标题】为啥我在运行简单的 Spring Boot 应用程序时总是得到状态为“404”的 Whitelabel 错误页面【英文标题】:Why do I always get Whitelabel Error Page with status "404" while running a simple Spring Boot Application为什么我在运行简单的 Spring Boot 应用程序时总是得到状态为“404”的 Whitelabel 错误页面 【发布时间】:2017-01-30 10:25:51 【问题描述】:

我的控制器

@Controller
//@RequestMapping("/")
//@ComponentScan("com.spring")
//@EnableAutoConfiguration
public class HomeController 

    @Value("$framework.welcomeMessage")
    private String message;

    @RequestMapping("/hello")
    String home(ModelMap model) 
        System.out.println("hittin the controller...");
        model.addAttribute("welcomeMessage", "vsdfgfgd");
        return "Hello World!";
    

    @RequestMapping(value = "/indexPage", method = RequestMethod.GET)
    String index(ModelMap model) 
        System.out.println("hittin the index controller...");
        model.addAttribute("welcomeMessage", message);
        return "welcome";
    

    @RequestMapping(value = "/indexPageWithModel", method = RequestMethod.GET)
    ModelAndView indexModel(ModelMap model) 
        System.out.println("hittin the indexPageWithModel controller...");
        model.addAttribute("welcomeMessage", message);
        return new ModelAndView("welcome", model);
    

/WEB-INF/jsp 中的我的 JSP (welcome.jsp)(父文件夹是 WebContent)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Welcome to Spring Boot</title>
</head>

<body>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
Message: $message
</body>
</html>

我的 pom.xml

<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>SpringBootPlay</groupId>
    <artifactId>SpringBootPlay</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.0.RELEASE</version>
    </parent>
    <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>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-io</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>com.jcabi</groupId>
            <artifactId>jcabi-log</artifactId>
            <version>0.17</version>
        </dependency>
    </dependencies>
    <properties>
        <java.version>1.8</java.version>
        <start-class>com.spring.play.BootLoader</start-class>
        <main.basedir>$basedir/../..</main.basedir>
        <m2eclipse.wtp.contextRoot>/</m2eclipse.wtp.contextRoot>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <useSystemClassLoader>false</useSystemClassLoader>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

我的应用初始化程序

@EnableAutoConfiguration
@SpringBootApplication
@ComponentScan( "com.spring.controller" )
@PropertySources(value =  @PropertySource("classpath:/application.properties") )
public class BootLoader extends SpringBootServletInitializer 

    final static Logger logger = Logger.getLogger(BootLoader.class);

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) 
        return application.sources(BootLoader.class);
    

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

我什至在我的 pom.xml 中添加了 thymeleaf 依赖项。它仍然没有工作。当我点击localhost:8080/hello or /indexPage or /indexPageWithModel时,它总是说

白标错误页面

此应用程序没有显式映射 /error,因此您将其视为后备。

2016 年 9 月 21 日星期三 21:34:18 EDT 出现意外错误(类型=未找到,状态=404)。 ]/WEB-INF/jsp/welcome.jsp

我的应用程序.properties

spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp
framework.welcomeMessage=Welcome to Dashboard

请帮助我。谢谢!

【问题讨论】:

可能是你的包命名问题。检查这个答案https://***.com/questions/49034254/spring-boot-whitelabel-error-page-type-not-found-status-404 【参考方案1】:

这是几乎所有 Spring Boot 初学者都面临的最常见错误之一。

解决方案非常简单,您的 Bootstrap 类应该知道它应该引用的包或类路径,以便访问组件/控制器。因此,您需要指定如下:- @ComponentScan(basePackages= "org.test.controller")

P.S.- 这里的“org.test.controller”是我保存控制器的包的限定名称。

【讨论】:

不错!在我的情况下,它位于主类的同级包中。 我解决了将控制器移动到同一个包的问题。子包也可以。【参考方案2】:

我自己想出来的。

当我将我的动态 webproject 转换为 maven 项目时,它并没有以这种方式创建文件夹结构

src/main/java

src/main/resources

src/main/webapp

我自己手动创建并将jsp文件从WebContent/WEB-INF/jsp移动到src/main/webapp/WEB-INF/jsp并修改了项目属性中的Java build path

然后我重新启动embedded tomcat 并再次尝试。它奏效了。

【讨论】:

【参考方案3】:

Whitelabel 错误页面的一般故障排除指南:

为 Spring Web 处理启用跟踪日志记录。添加到您的application.propertieslogging.level.org.springframework.web.*=TRACE

有了这个,您可以在启动期间查看您的控制器是否已正确注册。例如,您应该在日志中看到带有一个映射 GET 方法的 HelloController:

2020-08-20 08:56:55.731 TRACE 19687 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping :
    c.n.g.c.HelloController:
    GET /hello: sayHello()

如果您看到记录了您的控制器方法,则它已正确注册。如果没有,请遵循为正确的项目结构提供的其他建议。

如果控制器已正确注册,但仍然出现白标错误页面,则可能是以下情况之一:

您使用错误的 url/方法/内容类型调用端点 您的端点发生错误 您没有返回正确的响应(这可能导致 Spring 显示错误页面)

这些情况中的哪一种,也应该从生成的日志中揭示出来。狩猎愉快!

【讨论】:

【参考方案4】:

此类错误的原因是由于 Bootstrap 类不知道需要查看的控制器的位置。

在这种情况下,我们需要指定可以使用的包或类路径 @ComponentScan(basePackages="com.sample.controller") 在我的情况下,我将包指定为com.sample.controller

【讨论】:

【参考方案5】:

有时可能是因为缺少@Controller 或@RestController 注解。

Sample Program With @RestController Annotation

【讨论】:

虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接答案可能会失效。 - From Review

为啥spring-boot应用程序不需要@EnableWebMvc

】为啥spring-boot应用程序不需要@EnableWebMvc【英文标题】:whyspring-bootapplicationdoesn\'trequire@EnableWebMvc为什么spring-boot应用程序不需要@EnableWebMvc【发布时间】:2018-12-0303:19:16【问题描述】:所以我写了一个小应用程序,为了熟悉基础... 查看详情

为啥 spring-boot 应用程序无法在 AWS 中运行,但在本地运行良好?

】为啥spring-boot应用程序无法在AWS中运行,但在本地运行良好?【英文标题】:Whyspring-bootappisnotworkinginAWSbutworksfineinlocal?为什么spring-boot应用程序无法在AWS中运行,但在本地运行良好?【发布时间】:2020-12-1817:19:26【问题描述】... 查看详情

为啥我在 Spring-boot 应用程序中通过 SessionFactory 有循环依赖?

】为啥我在Spring-boot应用程序中通过SessionFactory有循环依赖?【英文标题】:WhydoIhaveacirculardependencythroughSessionFactoryinSpring-bootapplication?为什么我在Spring-boot应用程序中通过SessionFactory有循环依赖?【发布时间】:2020-02-0200:52:48【问... 查看详情

为啥 Spring Boot 2.0 应用程序不运行 schema.sql?

】为啥SpringBoot2.0应用程序不运行schema.sql?【英文标题】:WhySpringBoot2.0applicationdoesnotrunschema.sql?为什么SpringBoot2.0应用程序不运行schema.sql?【发布时间】:2018-09-0111:20:45【问题描述】:当我使用SpringBoot1.5时,在应用程序启动时,... 查看详情

无法加载在 Docker 中运行的 Spring Boot 应用程序

....9中运行它,构建1752eb3。该应用程序在本地运行,但是当我在Docker中运行 查看详情

为啥这个 Spring Boot 应用程序找不到主页?

】为啥这个SpringBoot应用程序找不到主页?【英文标题】:Whycan\'tthisSpringBootapplicationfindthemainpage?为什么这个SpringBoot应用程序找不到主页?【发布时间】:2018-01-1504:30:09【问题描述】:我正在尝试创建一个允许我使用的最小SpringBo... 查看详情

为啥我的 Spring Boot Web 应用程序无法在 Gradle 中完全运行?

】为啥我的SpringBootWeb应用程序无法在Gradle中完全运行?【英文标题】:WhydoesmySpringBootwebappnotruncompletelyinGradle?为什么我的SpringBootWeb应用程序无法在Gradle中完全运行?【发布时间】:2016-04-1522:12:13【问题描述】:我一直在尝试使... 查看详情

无法从通过intellij运行的spring boot应用程序连接到我在docker上运行的kafka

】无法从通过intellij运行的springboot应用程序连接到我在docker上运行的kafka【英文标题】:Can\'tconnecttomykafkarunningondockerfromspringbootapplicationrunningviaintellij【发布时间】:2022-01-0416:48:43【问题描述】:我有一个包含Kafka、zookeeper和spring... 查看详情

Spring Boot 不适用于 Elastic Beanstalk

...ws的新手。我构建了一个带有2个端点的简单应用程序,当我在我的Mac上本地运行它时,它运行良好。我在部署应用程序时遵循了Spring的说明。我设置了一个环境,生成了一个war文件,然后上传了它。在 查看详情

Amazon-Guard-Duty 用于我在 AWS 上运行的 Spring Boot 应用程序

】Amazon-Guard-Duty用于我在AWS上运行的SpringBoot应用程序【英文标题】:Amazon-Guard-DutyformyspringbootapplicationrunningonAWS【发布时间】:2018-12-0214:40:54【问题描述】:我有一个在AWS的EC2实例中运行的SpringBoot应用程序。它基本上为其他应用... 查看详情

运行 Spring Boot 简单应用程序时找不到 Oracle 驱动程序

】运行SpringBoot简单应用程序时找不到Oracle驱动程序【英文标题】:Oracledrivernotfoundwhilerunningspringbootsimpleapp【发布时间】:2020-02-0801:00:37【问题描述】:我已经通过start.spring.io并获得了添加了web和jpa依赖项的示例gradle应用程序,... 查看详情

如果我们重新启动 Spring Boot 应用程序,正在运行的用户线程发生了啥?

】如果我们重新启动SpringBoot应用程序,正在运行的用户线程发生了啥?【英文标题】:IfwerestartSpringBootapplication,whathappenedtotherunninguserthread?如果我们重新启动SpringBoot应用程序,正在运行的用户线程发生了什么?【发布时间】:20... 查看详情

为啥我的 Spring Boot 应用程序会自行关闭

】为啥我的SpringBoot应用程序会自行关闭【英文标题】:WhydoesmySpringBootappshutdownbyitself为什么我的SpringBoot应用程序会自行关闭【发布时间】:2016-06-2306:58:57【问题描述】:我使用java-jarxxx.jar启动我的SpringBootweb应用程序,但运行一... 查看详情

从子模块运行 Spring Boot 应用程序

...s)应用程序模块包含一个用@SpringBootApplication注解的类,当我在这个目录中时(MainModule/ 查看详情

如何将 spring-boot 作为客户端应用程序运行?

...entapplication?【发布时间】:2017-01-0510:28:21【问题描述】:我在单个应用程序中有2个主要入口点。第一个main启动服务器,映射控制器并启动一些工作线程。这些工作人员从云队列接收消息。如果负载增加,我希望能够增加额外的... 查看详情

当我在 Docker 容器中使用 kafka 运行 Spring Boot 应用程序时,代理不可用

】当我在Docker容器中使用kafka运行SpringBoot应用程序时,代理不可用【英文标题】:BrokernotavailablewhenIrunspringbootapplicationwithkafkainsideDockercontainer【发布时间】:2021-05-2313:45:06【问题描述】:我是docker新手,在Docker容器内运行Kafka代... 查看详情

Spring Boot 不运行单元测试

】SpringBoot不运行单元测试【英文标题】:Springbootdoesn\'trununittests【发布时间】:2017-02-2008:15:03【问题描述】:如何在使用springboot:run命令构建和部署时为SpringBoot应用程序运行单元测试。我的期望是在运行应用程序之前执行我的... 查看详情

为啥我在 Spring Boot 中从服务器端配置并做出反应时出现 CORS 错误?

】为啥我在SpringBoot中从服务器端配置并做出反应时出现CORS错误?【英文标题】:WhyIamgettingCORSerrorthoughiconfiguredfromserversideinspringbootandreact?为什么我在SpringBoot中从服务器端配置并做出反应时出现CORS错误?【发布时间】:2021-09-2218... 查看详情