Spring boot 开发Web程序的时候,控制器类所属的包,必须是主程序包之下一级的包。否则,无论加什么注解都没有用的!!
package com.example.demo20210413a;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Demo20210413aApplication {
public static void main(String[] args) {
SpringApplication.run(Demo20210413aApplication.class, args);
}
}
主程序包:com.example.demo20210413a
所以,控制器的包:com.example.demo20210413a.controller
package com.example.demo20210413a.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class TestController {
@ResponseBody
@RequestMapping("/hello")
public String hello() {
return "Hello";
}
}