Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java spring controller is not being mapped

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.

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):

...
...
[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):

...
...
[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:

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)

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

like image 804
CrazySabbath Avatar asked Oct 30 '22 20:10

CrazySabbath


1 Answers

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.

like image 91
CrazySabbath Avatar answered Nov 15 '22 05:11

CrazySabbath