Spring Boot 控制器不映射

新手上路,请多包涵

我已经使用了 STS,现在我正在使用 IntelliJ Ultimate Edition,但我仍然得到相同的输出。我的控制器没有被映射,因此显示 404 错误。我是 Spring Framework 的新手。

演示应用程序.java

 package com.webservice.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

HelloController.java

 package com.webservice.demo;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String sayHello(){
        return "Hey";
    }

}

控制台输出

com.webservice.demo.DemoApplication      : Starting DemoApplication on XFT000159365001 with PID 11708 (started by Mayank Khursija in C:\Users\Mayank Khursija\IdeaProjects\demo)
    2017-07-19 12:59:46.150  INFO 11708 --- [           main] com.webservice.demo.DemoApplication      : No active profile set, falling back to default profiles: default
    2017-07-19 12:59:46.218  INFO 11708 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@238e3f: startup date [Wed Jul 19 12:59:46 IST 2017]; root of context hierarchy
    2017-07-19 12:59:47.821  INFO 11708 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8211 (http)
    2017-07-19 12:59:47.832  INFO 11708 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
    2017-07-19 12:59:47.832  INFO 11708 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.15
    2017-07-19 12:59:47.944  INFO 11708 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
    2017-07-19 12:59:47.944  INFO 11708 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1728 ms
    2017-07-19 12:59:47.987  INFO 11708 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
    2017-07-19 12:59:48.510  INFO 11708 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
    2017-07-19 12:59:48.519  INFO 11708 --- [           main] o.s.c.support.DefaultLifecycleProcessor  : Starting beans in phase 0
    2017-07-19 12:59:48.634  INFO 11708 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8211 (http)
    2017-07-19 12:59:48.638  INFO 11708 --- [           main] com.webservice.demo.DemoApplication      : Started DemoApplication in 2.869 seconds (JVM running for 3.44)

原文由 Mayank 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 546
2 个回答

我也有类似的问题,并能够通过更正源包结构来最终解决 这个问题

组件扫描不会扫描您的控制器类。您的 Controller 类必须嵌套在具有 main() 方法的主 SpringApplication 类的包层次结构下方,然后只会扫描它,并且您还应该在 Spring Boot 启动时看到控制台输出中列出的 RequestMappings。

在 Spring Boot 1.5.8.RELEASE 上测试

但如果您更喜欢使用自己的包装结构,您始终可以使用 @ComponentScan 注释来定义您的 basePackages 进行扫描。

原文由 iamharish15 发布,翻译遵循 CC BY-SA 3.0 许可协议

因为 DemoApplication.classHelloController.class 在同一个包中

在其他类之上的根包中找到您的主应用程序类

查看 Spring Boot 文档 Locating the Main Application Class

使用根包还允许组件扫描仅应用于您的项目。

例如,在您的情况下,它如下所示:

com.webservice.demo.DemoApplication

com.webservice.demo.controller.HelloController

原文由 tsarenkotxt 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题