解决方法:
1.将controller的Java类放在启动类包(就是DemoApplication所在的包)下。
2.在启动类填加注解@ComponentScan,或者@ComponentScans
spring boot启动类 位于com.example.demo
package com.example.demo;
//在demo包下
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
//spring boot启动类
@SpringBootApplication
@ComponentScan("com.example.controller") //就是这句 括号里是要扫描的包
public class DemoApplication {
public static void main(String[] args) {
//启动内置tomcat
SpringApplication.run(DemoApplication.class, args);
}
}
要执行的controller 位于com.example.controller
package com.example.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.po.TUser;
@R