在 Spring Boot 1.5.3v 中的名称为 'dispatcherServlet' 的 DispatcherServlet 中找不到带有 URI [/welcome] 的 HTTP 请求的映射

No mapping found for HTTP request with URI [/welcome] in DispatcherServlet with name 'dispatcherServlet' in Spring Boot 1.5.3v

我是 spring 引导新手,无法解决问题。 在下面的屏幕截图中,我评论了“/hello”,我可以使用 localhost:1200/hello 访问它。但是无法访问其他控制器,因为“/welcome”,“/home”附加了屏幕截图。

SpringApplication class

Controller class

由于您的 @SpringBootApplication 注释在 com.example.demo 中,并且没有定义用于扫描的 basePackage,它只会扫描该包及其嵌套包。解决这个问题最简单的方法是移动你的控制器包。

来自这里:

.
└── main
    ├── java
    │   └── com
    │       └── example
    │           ├── controller
    │           │   └── DemoRestController.java
    │           └── demo
    │               └── DemoApplication.java
    └── resources
        └── application.properties

为此:

.
└── main
    ├── java
    │   └── com
    │       └── example
    │           └── demo
    │               ├── controller
    │               │   └── DemoRestController.java
    │               └── DemoApplication.java
    └── resources
        └── application.properties

来自 @ComponentScan 的文档:

Either basePackageClasses() or basePackages() (or its alias value()) may be specified to define specific packages to scan. If specific packages are not defined, scanning will occur from the package of the class that declares this annotation.