在我使用 rest controller 和 spring boot 编写了一个简单的路由之后,路由总是返回 404 是否 如果路线在单独的包裹或同一包裹中提及。 只有在提到 @SpringBootApplication 的主函数中定义了路由时,该路由才有效,请帮助我 包 com.gowtham;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@ComponentScan(basePackages = "com.gowtham")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@RestController
public class controller {
@GetMapping("/greet")
public String hello() {
return "HELLO";
}
}
/greet 路线有效
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ccc {
@GetMapping("/gg")
public String hello() {
return "HELLO";
}
}
但是位于主包下的 .controller 包内的这条路由 /gg 不起作用我尝试将类“ccc”移动到主包内,我也尝试扫描基本包但没有用
如果我在其他计算机上导入相同的程序,它运行良好
包结构
确保您的 Spring Boot 应用程序正在为您的控制器扫描正确的包。您可以通过在主类中添加@ComponentScan 注解来指定组件扫描的基础包。
@SpringBootApplication
@ComponentScan(basePackages = "com.gowtham")
public class DemoApplication {
// ...
}