我在參考spring官方guid中寫了一個小項目,由於idea將啟動項放在demo包下無法掃描
到java包下的controller類會報404。
解決方法:
1.將要用的組建放在啟動類包下(不規范)
2.在啟動類填加注解@ComponentScan,或者@ComponentScans
例:
package test.demo.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan("controller")//包名
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}