这是咋了,咋的就404了 我路径也挺对的啊 注解也都写上了啊 咋就找不到了呢? debug吧它不进方法 看日志吧,他还不报错 这家伙给我急的 百度一下午也没解决,最后还是看官网才知道错在了那里,程序只加载Application.java所在包及其子包下的内容。
解决方案:
一、在Application类中加上@ComponentScan(basePackages = {"com.demo.controller"}) 多个之间用","分隔 当然,这样治标不治本
1 package com.demo.example.demo; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 import org.springframework.context.annotation.ComponentScan; 6 7 @SpringBootApplication 8 @ComponentScan(basePackages = {"com.demo.controller"}) 9 public class Demo1Application { 10 11 public static void main(String[] args) { 12 SpringApplication.run(Demo1Application.class, args); 13 } 14 }
二、把包的目录结构修改成正确的
1 package com.demo; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 import org.springframework.context.annotation.ComponentScan; 6 7 @SpringBootApplication 8 public class Demo1Application { 9 10 public static void main(String[] args) { 11 SpringApplication.run(Demo1Application.class, args); 12 } 13 }