0

I'm working on a Spring Boot Application - and adding @RestController beans after the RequestMapping has already been initialized.
I can see the new Controller beans are in the web context, but since they were added after, there is no mapping when the application finishes starting.

I was hoping I could call "afterPropertiesSet()" on the RequestMappingHandlerMapping, but I end up with ambiguous mappings [I assume calling the method re-registers all beans with mappings]

Is there a way to register certain beans, or refresh the RequestMappingHandlerMapping?

Caused by: java.lang.IllegalStateException: Ambiguous handler methods mapped for HTTP path 'http://localhost:52765/error': {public org.springframework.http.ResponseEntity org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest), public org.springframework.http.ResponseEntity org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)}
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.lookupHandlerMethod(AbstractHandlerMethodMapping.java:367) ~[spring-webmvc-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.getHandlerInternal(AbstractHandlerMethodMapping.java:314) ~[spring-webmvc-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.getHandlerInternal(AbstractHandlerMethodMapping.java:61) ~[spring-webmvc-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.web.servlet.handler.AbstractHandlerMapping.getHandler(AbstractHandlerMapping.java:352) ~[spring-webmvc-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.getHandler(DispatcherServlet.java:1160) ~[spring-webmvc-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:940) ~[spring-webmvc-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901) ~[spring-webmvc-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) ~[spring-webmvc-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    ... 26 common frames omitted  

Asking another way -

I have a @RestController in a separate project that is not referenced by the configuration project.
I have a third project that references both the configuration project and the 'module' project with the @RestController.

How do I ensure the @RestController in the module project is added to the application context before the RequestMappingHandlerMapping is configured?

I created a custom annotation, thinking maybe I could use it somehow.. See the projects here -
Main platform - https://github.com/savantly-net/sprout-platform/tree/development/

Autoconfiguration project - https://github.com/savantly-net/sprout-platform/tree/development/spring/sprout-spring-boot-starter

Module Project - https://github.com/savantly-net/sprout-platform/tree/development/modules/sprout-wiki-module

Full Web Project - https://github.com/savantly-net/garden/tree/development

If you build this project, you will see the wiki module beans are added to the application context, but the @RestController is never mapped.

Any thoughts?

Jeremy
  • 2,970
  • 1
  • 26
  • 50

1 Answers1

0

I changed the module declaration in the web app from a @Bean to an @Import and it resolved the issue. I'm assuming this bumped up the order of the beans in the module project.

Before -

@SpringBootApplication
public class GardenWebApplication {

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

    @Bean
    public WikiModule wikiModule() {
        return new WikiModule();
    }
}

After -

@SpringBootApplication
@Import(WikiModule.class)
public class GardenWebApplication {

    public static void main(String[] args) {
        SpringApplication.run(GardenWebApplication.class, args);
    }
}
Jeremy
  • 2,970
  • 1
  • 26
  • 50