如何在 spring 框架上修复 "Whitelabel Error Page" - GET 请求不起作用
How to fix "Whitelabel Error Page" on spring framework - GET request is not working
我正在尝试使用 spring 启动 "Hello World" 控制器,但 GET 请求无法正常工作。我该怎么做才能解决这个问题?
我在 https://start.spring.io/ 处使用了 spring 初始化程序,在依赖项中我选择了 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"
我遇到过类似的问题。
我建议你从同一个包下的所有东西开始(例如将 initilizer 移到 com.example.hello),这很可能与项目结构有关。
这是一篇关于该问题的文章:
https://www.dev2qa.com/spring-boot-resolve-whitelabel-error-page-example/
引自上文link:
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 将扫描主应用程序下面的组件class。
在您的情况下,您的主控制器位于 package com.example.demo;
,您的控制器位于 package com.example.hello;
。
我建议您将控制器保存在 com.example.demo
包下的新包中,例如 com.example.demo.controller
按照 documentation.
中提到的结构
如果你真的想使用com.example.hello
包,你可能需要添加@ComponentScan("com.example.hello")
,这将变得难以维护。
可以找到更多信息 here。
我正在尝试使用 spring 启动 "Hello World" 控制器,但 GET 请求无法正常工作。我该怎么做才能解决这个问题?
我在 https://start.spring.io/ 处使用了 spring 初始化程序,在依赖项中我选择了 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"
我遇到过类似的问题。 我建议你从同一个包下的所有东西开始(例如将 initilizer 移到 com.example.hello),这很可能与项目结构有关。
这是一篇关于该问题的文章:
https://www.dev2qa.com/spring-boot-resolve-whitelabel-error-page-example/
引自上文link:
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 将扫描主应用程序下面的组件class。
在您的情况下,您的主控制器位于 package com.example.demo;
,您的控制器位于 package com.example.hello;
。
我建议您将控制器保存在 com.example.demo
包下的新包中,例如 com.example.demo.controller
按照 documentation.
如果你真的想使用com.example.hello
包,你可能需要添加@ComponentScan("com.example.hello")
,这将变得难以维护。
可以找到更多信息 here。