简体   繁体   English

如何修复 spring 框架上的“Whitelabel 错误页面”- GET 请求不起作用

[英]How to fix "Whitelabel Error Page" on spring framework - GET request is not working

I'm trying to make a "Hello World" controller with spring boot and the GET request is not working.我正在尝试使用 spring 引导创建“Hello World”controller,但 GET 请求无效。 What can i do to fix this?我该怎么做才能解决这个问题?

I used spring initializer at https://start.spring.io/ and in the Dependencies I chose web我在https://start.spring.io/使用了 spring 初始值设定项,在依赖项中我选择了 web

I've tried to use diffrent annotations like @GetMapping我试过使用不同的注释,比如@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);
    }

}

I want the output to be "Hello World" but the actual output is "Whitelabel Error Page"我希望 output 是“Hello World”,但实际的 output 是“Whitelabel Error Page”

The issue is related to your project structure, by default Spring Boot will scan the components below your main application class.该问题与您的项目结构有关,默认情况下 Spring Boot 将扫描您的主应用程序类下面的组件。

In your case your main is located at package com.example.demo;在您的情况下,您的 main 位于package com.example.demo; , and your controller is located at package com.example.hello; , 你的控制器位于package com.example.hello; . .

I would recommend you to keep your controller in a new package below the com.example.demo package, like com.example.demo.controller as per structure mentioned in the documentation .我建议您将控制器保存在com.example.demo包下方的新包中,例如com.example.demo.controller根据文档中提到的结构。

If you really want to use the com.example.hello package, you may need to add the @ComponentScan("com.example.hello") , which becomes hard to mantain in the future.如果你真的想使用com.example.hello包,你可能需要添加@ComponentScan("com.example.hello") ,这在未来变得难以维护。

More info can be found here .更多信息可以在这里找到。

I've had similar issues.我有过类似的问题。 I suggest you start with everything under the same package (move initilizer to com.example.hello for instance), it is most likely related to the project structure.我建议您从同一个包下的所有内容开始(例如将初始化程序移动到 com.example.hello),这很可能与项目结构有关。

Here is an article about the issue:这是一篇关于这个问题的文章:

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

Quote from link above:引用上面的链接:

So if you use @SpringBootApplication annotation in your spring boot main class, you must place the main class in root package as bellow to avoid whitelabel error page.因此,如果您在 spring boot 主类中使用 @SpringBootApplication 注释,则必须将主类如下放置在根包中,以避免出现白标错误页面。

In my case having the RequestMapping method helloWorld inside the DemoApplication Class wouldn't work.在我的例子中,在 DemoApplication Class 中使用 RequestMapping 方法 helloWorld 是行不通的。 When I split the methods by creating the HelloController Class and adding the helloWorld method to this Class, I got the page returning "Hello World".当我通过创建 HelloController Class 并将 helloWorld 方法添加到此 Class 来拆分方法时,我得到了返回“Hello World”的页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM