Spring Boot Controller没有映射

May*_*ank 18 java spring

我使用过STS,现在我正在使用IntelliJ Ultimate Edition,但我仍然得到相同的输出.我的控制器未映射,因此显示404错误.我是Spring Framework的新手.

DemoApplication.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);
    }
}
Run Code Online (Sandbox Code Playgroud)

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";
    }

}
Run Code Online (Sandbox Code Playgroud)

控制台输出

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)
Run Code Online (Sandbox Code Playgroud)

iam*_*h15 56

我也曾经有过类似的问题,并能够通过修正后的源代码包结构,以最终解决之

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

在Spring Boot 1.5.8.RELEASE上测试

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

  • 好极了!我认为这应该被标记为正确的答案 (5认同)
  • 哇,对于无声错误来说,这是一个不直观的解决方案.谢谢春天.应该至少有一些日志记录说"嘿,我们注意到你没有映射任何控制器.这是你应该看的一件事." 我的意思是,当然,实际的应用程序可能应该是你的层次结构的根源,但如果由于某种原因不是这种情况,那么Spring看起来只是让你干涸. (2认同)
  • 没有必要维护那个包结构,但如果你有自己的包结构,那么请确保用 `@ComponentScan` 注释你的 Main Application 类并提及你的 `basePackages` (2认同)

tsa*_*txt 13

因为DemoApplication.classHelloController.class在同一个包中
找到你的主应用程序类在其他类上面的根包中
查看Spring Boot文档查找主应用程序类

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

例如,在您的情况下,它看起来像下面:

com.webservice.demo.DemoApplication
com.webservice.demo.controller.HelloController


小智 6

在 @SpringBootApplication 上面的主类中添加 @ComponentScan(com.webservice) 将解决您的问题。参考下面的代码

package com.webservice.demo;

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

@ComponentScan(com.webservice)
@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}
Run Code Online (Sandbox Code Playgroud)


vis*_*213 6

就我而言,我使用的是@Controller而不是@RestControllerwith@RequestMapping


ptr*_*trk 5

就我而言,它缺少 pom.xml 的依赖项,否则一切都编译得很好。Spring 日志中的 404 和缺少的映射信息是唯一的提示。

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
Run Code Online (Sandbox Code Playgroud)


小智 5

我也遇到了类似的问题,并使用正确的包结构解决了它,如下所示。修正后,工作正常。例如

  • Spring应用程序主类位于com.example包中
  • 控制器类位于 com.example.controller 包中