Java spring 控制器没有被映射

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32246461/
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-11-02 19:53:38  来源:igfitidea点击:

Java spring controller is not being mapped

javaspringspring-mvctomcat

提问by CrazySabbath

I have a spring web application, which has few controllers. My problem is that while trying to redeploy .war to tomcat (or simply restarting application) controllers aren't being mapped from the first try. I have to redeploy app or restart again until they are finally mapped...I have no idea why this is happening.

我有一个 Spring Web 应用程序,它的控制器很少。我的问题是,在尝试将 .war 重新部署到 tomcat(或简单地重新启动应用程序)时,控制器并未从第一次尝试开始映射。我必须重新部署应用程序或重新启动,直到它们最终被映射......我不知道为什么会发生这种情况。

One of my controllers:

我的控制器之一:

@RestController
@RequestMapping("kt")
public class KTIntegrationController {

    @Autowired
    KtImportService importService;

    @ExceptionHandler(ItemNotFoundException.class)
    @ResponseStatus(value= HttpStatus.NOT_FOUND)
    public RestError handleNotFoundException(ItemNotFoundException e) {
        return new RestError(404, "NOT FOUND", e.getMessage());
    }

    @RequestMapping(value="import/to-db", method=RequestMethod.POST)
    public RestResponse<List<String>> loadToDB(@RequestParam("initiator") String initiator,
                            @RequestParam("process") String pid,
                            @RequestParam("category") String institution) throws KtImportException {
        return new RestResponse<List<String>>(importService.loadToDB(initiator,pid,institution));
    }

}

Tomcat log (when controller was not mapped):

Tomcat 日志(未映射控制器时):

...
...
[12:47:40.887] INFO  o.s.w.s.m.m.a.RequestMappingHandlerAdapter - Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@bf5b3f: startup date [Thu Aug 27 12:47:23 EEST 2015]; root of context hierarchy
[12:47:41.107] INFO  o.s.w.s.m.m.a.RequestMappingHandlerMapping - Mapped "{[/error],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
[12:47:41.108] INFO  o.s.w.s.m.m.a.RequestMappingHandlerMapping - Mapped "{[/error],methods=[],params=[],headers=[],consumes=[],produces=[text/html],custom=[]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest)
[12:47:41.173] INFO  o.s.w.s.h.SimpleUrlHandlerMapping - Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
[12:47:41.173] INFO  o.s.w.s.h.SimpleUrlHandlerMapping - Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
[12:47:41.291] INFO  o.s.w.s.h.SimpleUrlHandlerMapping - Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
[12:47:41.895] INFO  o.s.j.e.a.AnnotationMBeanExporter - Registering beans for JMX exposure on startup
[12:47:41.922] INFO  o.s.boot.SpringApplication - Started application in 19.463 seconds (JVM running for 547.311)
...
...

Tomcat log (when controller was mapped):

Tomcat 日志(映射控制器时):

...
...
[11:27:39.426] INFO  o.s.w.s.m.m.a.RequestMappingHandlerAdapter - Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@1722a662: startup date [Thu Aug 27 11:27:28 EEST 2015]; root of context hierarchy
[11:27:39.577] INFO  o.s.w.s.m.m.a.RequestMappingHandlerMapping - Mapped "{[/kt/import/to-db],methods=[POST],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public com.gerasolutions.rest.RestResponse<java.util.List<java.lang.String>> com.gera.integration.rc.rest.controller.KTIntegrationController.loadToDB(java.lang.String,java.lang.String,java.lang.String) throws com.gera.integration.rc.core.domain.KtImportException
...
...
[11:27:39.586] INFO  o.s.w.s.m.m.a.RequestMappingHandlerMapping - Mapped "{[/error],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
[11:27:39.586] INFO  o.s.w.s.m.m.a.RequestMappingHandlerMapping - Mapped "{[/error],methods=[],params=[],headers=[],consumes=[],produces=[text/html],custom=[]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest)
[11:27:39.634] INFO  o.s.w.s.h.SimpleUrlHandlerMapping - Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
[11:27:39.635] INFO  o.s.w.s.h.SimpleUrlHandlerMapping - Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
[11:27:39.706] INFO  o.s.w.s.h.SimpleUrlHandlerMapping - Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
[11:27:40.009] INFO  o.s.j.e.a.AnnotationMBeanExporter - Registering beans for JMX exposure on startup
[11:27:40.022] INFO  o.s.boot.SpringApplication - Started application in 12.283 seconds (JVM running for 19.89)
...
...

I don't expect to find answer how to fix this here, since this is a pretty confusing bug (well, at least for me), but some ideas or pointers where I could start looking would be most helpeful.

我不希望在这里找到如何解决这个问题的答案,因为这是一个非常令人困惑的错误(好吧,至少对我来说),但是我可以开始寻找的一些想法或指示将是最有帮助的。

--EDIT--

- 编辑 -

Project structure:

项目结构:

app
    -> src
        -> main
            -> java
                -> com.app.a
                    -> c
                        -> config
                            -> Config.class
                    -> r
                        -> controller
                            -> Controller.class
                    Application.class
                    ServletInitialiazer.class
            -> resources
            -> webapp (this is empty)

Application.class:

应用程序类:

@Configuration
@ComponentScan(basePackages={"com.app.a.*"})
@EnableAutoConfiguration
public class Application {

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

ServletInitializer.class:

ServletInitializer.class:

public class ServletInitializer extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

}

Config.class has datasource and other beans defined in it. (shouldn't affect this bug)

Config.class 中定义了数据源和其他 bean。(应该不会影响这个bug)

--EDIT--

- 编辑 -

What's more, if I simply boot this application .war from my laptop, using command line: mvn spring-boot:run, everything works, controllers are always being mapped from the first try.

更重要的是,如果我只是从我的笔记本电脑启动这个应用程序 .war,使用命令行:mvn spring-boot:run,一切正常,控制器总是从第一次尝试开始映射。

采纳答案by CrazySabbath

Solution to the problem:

问题的解决方法:

My spring web application is using few java projects (included jars through maven) that I wrote. Problem was, that included jars, which are used like typical java libraries, where in fact spring-boot projects. Removing spring-boot from java projects, that were using spring-boot, solved this issue.

我的spring web应用程序使用了我编写的几个java项目(包括通过maven的jar)。问题是,这包括 jars,它像典型的 java 库一样使用,实际上是 spring-boot 项目。从使用 spring-boot 的 java 项目中删除 spring-boot 解决了这个问题。

回答by fillup

One thing which I've found to work in the past with these type of errors is to change the granularity of the package scanning:

我发现过去可以解决这些类型的错误的一件事是更改包扫描的粒度:

e.g.

例如

@ComponentScan(basePackages={"com.app"})

I know it's bizarre but it's worked for me in the past. I've no idea why this happens though.

我知道这很奇怪,但它过去对我有用。我不知道为什么会发生这种情况。