如何在Spring框架上修复“Whitelabel Error Page” - GET请求无法工作

3
我正在尝试使用Spring Boot制作“Hello World”控制器,但GET请求无法正常工作。我该怎么做才能解决这个问题?
我在https://start.spring.io/上使用了Spring Initializer,并在依赖项中选择了web。
我尝试使用不同的注释,如@GetMapping。请帮忙看看哪里出了问题。
package com.example.hello;

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

@RestController
public class HelloController {

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


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

    <properties>
        <java.version>1.8</java.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>


package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

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

}


我想输出"Hello World",但实际输出是"Whitelabel Error Page"。请将此句话翻译成中文。

你确定要调用 http://localhost:8080/a 吗?我这边运行得很好。 - undefined
1
项目的结构是什么样子的? - user10871691
你还可以尝试这个入门项目,它是一个完美的工作示例,完全符合你的需求。它甚至内置了Tomcat,所以你只需运行主类,就可以在浏览器中访问你的控制器。 https://spring.io/guides/gs/spring-boot/ - undefined
告诉我控制器的合格名称和主类的合格名称。 - user10871691
@EugenCovaci 控制器类的名称是HelloController,主类的名称是DemoApplication。 - undefined
显示剩余4条评论
3个回答

11
问题与您的项目结构有关,Spring Boot默认会扫描主应用程序类下面的组件。
在您的情况下,您的主类位于“com.example.demo”包中,而您的控制器位于“com.example.hello”包中。
我建议您将控制器保存在新的包中,该包位于“com.example.demo”包下面,例如“com.example.demo.controller”,根据文档中提到的结构。
如果您确实想使用“com.example.hello”包,则可能需要添加“@ComponentScan(“com.example.hello”)”,这在未来会变得难以维护。
更多信息可以在这里找到。

1
我遇到过类似的问题。 我建议你将所有内容放在同一个包中(例如,将初始化器移动到com.example.hello),这很可能与项目结构有关。
以下是一篇关于此问题的文章:

https://www.dev2qa.com/spring-boot-resolve-whitelabel-error-page-example/

摘自上面的链接:

因此,如果您在Spring Boot主类中使用@SpringBootApplication注释,则必须将主类放置在根包中,以避免白标签错误页。


同意。我刚刚遇到了这个错误,只是一个复制粘贴的问题,Controller类在顶部缺少了一个包定义。 - undefined

0
在我的情况下,将RequestMapping方法helloWorld放在DemoApplication类中是行不通的。当我通过创建HelloController类并将helloWorld方法添加到该类中来拆分方法时,页面返回了“Hello World”。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接