简体   繁体   English

Spring 启动 Controller 不映射

[英]Spring Boot Controller not mapping

I have used STS and now I am using IntelliJ Ultimate Edition but I am still getting the same output.我使用过 STS,现在我使用的是 IntelliJ Ultimate Edition,但我仍然得到相同的 output。 My controller is not getting mapped thus showing 404 error.我的 controller 没有被映射,因此显示 404 错误。 I am completely new to Spring Framework.我对 Spring 框架完全陌生。

DemoApplication.java 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);
    }
}

HelloController.java 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";
    }

}

Console Output控制台 Output

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)

I too had the similar issue and was able to finally resolve it by correcting the source package structure following this我也有类似的问题,并且能够通过在之后更正源包结构来最终解决它

Your Controller classes are not scanned by the Component scanning.组件扫描不会扫描您的控制器类。 Your Controller classes must be nested below in package hierarchy to the main SpringApplication class having the main() method, then only it will be scanned and you should also see the RequestMappings listed in the console output while Spring Boot is getting started.您的 Controller 类必须在包层次结构中嵌套到具有 main() 方法的主 SpringApplication 类的下方,然后只会扫描它,并且在 Spring Boot 开始时,您还应该看到控制台输出中列出的 RequestMappings。

Tested on Spring Boot 1.5.8.RELEASE在 Spring Boot 1.5.8.RELEASE 上测试

But in case you prefer to use your own packaging structure, you can always use the @ComponentScan annotation to define your basePackages to scan.但是,如果您更喜欢使用自己的打包结构,您始终可以使用@ComponentScan注释来定义要扫描的basePackages

Because of DemoApplication.class and HelloController.class in the same package因为DemoApplication.classHelloController.class在同一个包中
Locate your main application class in a root package above other classes在其他类上方的根包中找到您的主应用程序类
Take look at Spring Boot documentation Locating the Main Application Class查看 Spring Boot 文档定位主应用程序类

Using a root package also allows component scan to apply only on your project.使用根包还允许组件扫描仅应用于您的项目。

For example, in your case it looks like below:例如,在您的情况下,它如下所示:

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

In my case, it was missing the dependency from pom.xml, otherwise everything compiled just fine.就我而言,它缺少来自 pom.xml 的依赖项,否则一切都编译得很好。 The 404 and missing mappings info from Spring logs were the only hints. Spring 日志中的 404 和缺少的映射信息是唯一的提示。

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

I also had trouble with a similar issue and resolved it using the correct package structure as per below.我也遇到了类似问题,并使用正确的包结构解决了它,如下所示。 After correction, it is working properly.修正后,它可以正常工作。 eg例如

  • Spring Application Main Class is in package com.example Spring Application Main Class 在 com.example 包中
  • Controller Classes are in package com.example.controller控制器类在 com.example.controller 包中

Adding @ComponentScan(com.webservice) in main class above @SpringBootApplication will resolve your problem.在@SpringBootApplication 上方的主类中添加@ComponentScan(com.webservice) 将解决您的问题。 Refer below code参考下面的代码

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

When I was creating new project, did Dependency check Web >> Jersey (JAX-RS), and I got in pom.xml: 当我创建新项目时,依赖检查Web >> Jersey(JAX-RS),我进入pom.xml:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jersey</artifactId>
</dependency>

Everything was compiling, no mapping in log, same situation as yours. 一切都在编译,日志中没有映射,与你的情况相同。

So, I manually added in pox.xml: 所以,我在pox.xml中手动添加:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

And everything start working. 一切都开始奏效了。

After that I created new project with Dependency only Web >> Web checking in wizard, and I got what I want from beginning. 之后我创建了一个新的项目,只使用了依赖Web >>向导中的Web检查,我从头开始就得到了我想要的东西。

In my case I did not have to do anything with server.contextPath neither method . 在我的情况下,我没有做任何与server.contextPath做任何方法的事情。

In my opinion, this visibility problem comes when we leave the component scan to Spring which has a particular way of looking for the classes using standard convention.在我看来,当我们将组件扫描留给 Spring 时,就会出现这种可见性问题,Spring 具有使用标准约定查找类的特定方式。 In this scenario as the Starter class(DemoApplication)is in com.webservice.demo package, putting Controller one level below will help Spring to find the classes using the default component scan mechanism.在这种情况下,由于 Starter 类(DemoApplication)在 com.webservice.demo 包中,将 Controller 放在下面一层将有助于 Spring 使用默认的组件扫描机制找到类。 Putting HelloController under com.webservice.demo.controller should solve the issue.将 HelloController 放在 com.webservice.demo.controller 下应该可以解决问题。

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

It depends on a couple of properties:这取决于几个属性:

  • server.contextPath property in application properties.应用程序属性中的server.contextPath属性。 If it's set to any value then you need to append that in your request url.如果它设置为任何值,那么您需要将其附加到您的请求网址中。 If there is no such property then add this line in application.properties server.contextPath=/如果没有这样的属性,那么在 application.properties server.contextPath=/中添加这一行
  • method property in @RequestMapping , there does not seem to be any value and hence, as per documentation , it should map to all the methods. @RequestMapping中的method属性,似乎没有任何值,因此,根据文档,它应该映射到所有方法。 However, if you want it to listen to any particular method then you can set it to let's say method = HttpMethod.GET但是,如果你想让它监听任何特定的方法,那么你可以将它设置为让我们说method = HttpMethod.GET

I found the answer to this.我找到了答案。 This was occurring because of security configuration which is updated in newer versions of Spring Framework.这是因为在较新版本的 Spring Framework 中更新了安全配置。 So i just changed my version from 1.5.4 to 1.3.2所以我只是将我的版本从 1.5.4 更改为 1.3.2

在我的情况下,我使用错误的端口进行测试请求 - Tomcat 启动时暴露了几个端口(包括一个用于监视 /actuator)。

In my case I changed the package of configuration file.就我而言,我更改了配置文件的包。 Moved it back to the original com.example.demo package and things started working.把它移回原来的 com.example.demo 包,事情就开始工作了。

Another case might be that you accidentally put a Java class in a Kotlin sources directory as I did.另一种情况可能是您像我一样不小心将 Java 类放在了 Kotlin 源目录中。

Wrong:错误的:

src/main
┕ kotlin       ← this is wrong for Java
  ┕ com
    ┕ example
        ┕ web
          ┕ Controller.class

Correct:正确的:

src/main
┕ java          ← changed 'kotlin' to 'java'
  ┕ com
    ┕ example
        ┕ web
          ┕ Controller.class

Because when in Kotlin sources directory, Java class won't get picked up.因为在 Kotlin 源目录中时,Java 类不会被拾取。

All other packages should be an extension of parent package then only spring boot app will scan them by default.所有其他包应该是父 package 的扩展,然后默认情况下只有 spring 引导应用程序会扫描它们。

Other option will be to use @ComponentScan(com.webservice)其他选择是使用@ComponentScan(com.webservice)

package structure package结构

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM