Java Spring Boot 控制器未映射

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45183875/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 01:52:40  来源:igfitidea点击:

Spring Boot Controller not mapping

javaspring

提问by Mayank

I have used STS and now I am using IntelliJ Ultimate Edition but I am still getting the same output. My controller is not getting mapped thus showing 404 error. I am completely new to Spring Framework.

我使用过 STS,现在我使用的是 IntelliJ Ultimate Edition,但我仍然得到相同的输出。我的控制器没有得到映射,因此显示 404 错误。我对 Spring Framework 完全陌生。

DemoApplication.java

演示应用程序.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

你好控制器.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

控制台输出

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)

采纳答案by iamharish15

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.

组件扫描不会扫描您的控制器类。您的控制器类必须在包层次结构中嵌套在具有 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 @ComponentScanannotation to define your basePackagesto scan.

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

回答by Darshan Mehta

It depends on a couple of properties:

这取决于几个属性:

  • server.contextPathproperty in application properties. 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=/
  • methodproperty in @RequestMapping, there does not seem to be any value and hence, as per documentation, it should map to all the methods. However, if you want it to listen to any particular method then you can set it to let's say method = HttpMethod.GET
  • server.contextPath应用程序属性中的属性。如果它设置为任何值,那么您需要将其附加到您的请求 url 中。如果没有这样的属性,则在 application.properties 中添加这一行server.contextPath=/
  • method中的属性@RequestMapping,似乎没有任何值,因此,根据文档,它应该映射到所有方法。但是,如果您希望它收听任何特定方法,那么您可以将其设置为让我们说method = HttpMethod.GET

回答by Mayank

I found the answer to this. This was occurring because of security configuration which is updated in newer versions of Spring Framework. So i just changed my version from 1.5.4 to 1.3.2

我找到了这个问题的答案。这是因为在较新版本的 Spring Framework 中更新了安全配置。所以我只是将我的版本从 1.5.4 更改为 1.3.2

回答by ptrk

In my case, it was missing the dependency from pom.xml, otherwise everything compiled just fine. The 404 and missing mappings info from Spring logs were the only hints.

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

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

回答by tsarenkotxt

Because of DemoApplication.classand HelloController.classin the same package
Locate your main application class in a root package above other classes
Take look at Spring Boot documentation Locating the Main Application Class

由于DemoApplication.classHelloController.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

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

回答by Chinmay Biswal

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. 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. Putting HelloController under com.webservice.demo.controller should solve the issue.

在我看来,当我们将组件扫描留给 Spring 时,就会出现这种可见性问题,Spring 具有使用标准约定查找类的特定方式。在这个场景中,由于 Starter 类(DemoApplication)在 com.webservice.demo 包中,将 Controller 放在下面一层将帮助 Spring 使用默认的组件扫描机制找到类。将 HelloController 放在 com.webservice.demo.controller 下应该可以解决问题。

回答by volkovs

In my case I used wrong port for test request - Tomcat was started with several ones exposed (including one for monitoring /actuator).

就我而言,我对测试请求使用了错误的端口 - Tomcat 启动时暴露了几个端口(包括一个用于监控 /actuator)。

回答by Jayesh Patel

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. e.g.

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

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

回答by Gaurav Gupta

Adding @ComponentScan(com.webservice) in main class above @SpringBootApplication will resolve your problem. Refer below code

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