嗨,我正在学习 Spring,我不明白为什么我会收到白标签错误
主类:
package com.hello.springhelloworld;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringHelloWorldApplication {
public static void main(String[] args) {
SpringApplication.run(SpringHelloWorldApplication.class, args);
}
}
Hello World :
package web;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class HelloWorldController {
@RequestMapping("/hello")
public String sayHello(@RequestParam(value = "name") String name){
return "Hello" + name + "!";
}
}
最佳答案
Spring Boot 的组件扫描不是在扫描您的 Controller 类。
您的 Controller 类应该与您的 SpringHelloWorldApplication 类在同一个包中(或在子包中)。
所以你的 Controller 包声明应该是:
package com.hello.springhelloworld.web;
或
package com.hello.springhelloworld;
Spring Boot 的默认组件扫描会扫描同一个包或主应用程序类包的子包上的组件。
关于java - 为什么我从 hello world SpringBoot 收到 WhiteLabel 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51501166/