2

I am using a custom annotation called @Toggle on top of a rest controller class in spring boot to intercept the handlerType to block rest calls.

See below code snippet:

@RestController
@Toggle(feature="feature.foo")
@RequestMapping("/foo")
public class FooController {
  @RequestMapping("")
  public Map hello() {
    return Collections.singletonMap("message", "hello foo!");
  }
}

But I am not able to access the api which fails even before dispatcher servlet find the handler method.

2018-02-17 18:51:31.520 DEBUG 73947 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/foo]
2018-02-17 18:51:31.521 DEBUG 73947 --- [nio-8080-exec-2] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /foo
2018-02-17 18:51:31.523 DEBUG 73947 --- [nio-8080-exec-2] s.w.s.m.m.a.RequestMappingHandlerMapping : Did not find handler method for [/foo]
2018-02-17 18:51:31.523 DEBUG 73947 --- [nio-8080-exec-2] o.s.w.s.handler.SimpleUrlHandlerMapping  : Matching patterns for request [/foo] are [/**]
2018-02-17 18:51:31.523 DEBUG 73947 --- [nio-8080-exec-2] o.s.w.s.handler.SimpleUrlHandlerMapping  : URI Template variables for request [/foo] are {}
2018-02-17 18:51:31.524 DEBUG 73947 --- [nio-8080-exec-2] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapping [/foo] to HandlerExecutionChain with handler [ResourceHttpRequestHandler [locations=[ServletContext resource [/], class path resource [META-INF/resources/], class path resource [resources/], class path resource [static/], class path resource [public/]], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@333c8791]]] and 1 interceptor
2018-02-17 18:51:31.524 DEBUG 73947 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : Last-Modified value for [/foo] is: -1
2018-02-17 18:51:31.525 DEBUG 73947 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
2018-02-17 18:51:31.525 DEBUG 73947 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : Successfully completed request
2018-02-17 18:51:31.529 DEBUG 73947 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/error]
2018-02-17 18:51:31.529 DEBUG 73947 --- [nio-8080-exec-2] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /error
2018-02-17 18:51:31.529 DEBUG 73947 --- [nio-8080-exec-2] s.w.s.m.m.a.RequestMappingHandlerMapping : Returning handler method [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)]
2018-02-17 18:51:31.530 DEBUG 73947 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : Last-Modified value for [/error] is: -1

Found below logs which can throw some more light on the problem.

2018-02-17 20:30:24.172 DEBUG 94537 --- [           main] .i.s.PathMatchingResourcePatternResolver : Resolved location pattern [classpath*:in/karthick/spring/**/*.class] to resources [file [/Users/karthick/feature-toggles/target/classes/in/karthick/spring/AppConfig.class], file [/Users/karthick/feature-toggles/target/classes/in/karthick/spring/Constants.class], file [/Users/karthick/feature-toggles/target/classes/in/karthick/spring/FeatureToggleApplication.class], file [/Users/karthick/feature-toggles/target/classes/in/karthick/spring/web/FooController.class], file [/Users/karthick/feature-toggles/target/classes/in/karthick/spring/web/HelloController.class]]
2018-02-17 20:30:24.172 TRACE 94537 --- [           main] o.s.c.a.ClassPathBeanDefinitionScanner   : Scanning file [/Users/karthick/feature-toggles/target/classes/in/karthick/spring/AppConfig.class]
2018-02-17 20:30:24.190 DEBUG 94537 --- [           main] o.s.c.a.ClassPathBeanDefinitionScanner   : Identified candidate component class: file [/Users/karthick/feature-toggles/target/classes/in/karthick/spring/AppConfig.class]
2018-02-17 20:30:24.191 TRACE 94537 --- [           main] o.s.c.a.ClassPathBeanDefinitionScanner   : Scanning file [/Users/karthick/feature-toggles/target/classes/in/karthick/spring/Constants.class]
2018-02-17 20:30:24.191 TRACE 94537 --- [           main] o.s.c.a.ClassPathBeanDefinitionScanner   : Ignored because not matching any filter: file [/Users/karthick/feature-toggles/target/classes/in/karthick/spring/Constants.class]
2018-02-17 20:30:24.191 TRACE 94537 --- [           main] o.s.c.a.ClassPathBeanDefinitionScanner   : Scanning file [/Users/karthick/feature-toggles/target/classes/in/karthick/spring/FeatureToggleApplication.class]
2018-02-17 20:30:24.196 TRACE 94537 --- [           main] o.s.c.a.ClassPathBeanDefinitionScanner   : Ignored because not matching any filter: file [/Users/karthick/feature-toggles/target/classes/in/karthick/spring/FeatureToggleApplication.class]
2018-02-17 20:30:24.196 TRACE 94537 --- [           main] o.s.c.a.ClassPathBeanDefinitionScanner   : Scanning file [/Users/karthick/feature-toggles/target/classes/in/karthick/spring/web/FooController.class]
2018-02-17 20:30:24.205 TRACE 94537 --- [           main] o.s.c.e.PropertySourcesPropertyResolver  : Searching for key 'feature.foo' in [servletConfigInitParams]
2018-02-17 20:30:24.205 TRACE 94537 --- [           main] o.s.c.e.PropertySourcesPropertyResolver  : Searching for key 'feature.foo' in [servletContextInitParams]
2018-02-17 20:30:24.205 TRACE 94537 --- [           main] o.s.c.e.PropertySourcesPropertyResolver  : Searching for key 'feature.foo' in [systemProperties]
2018-02-17 20:30:24.206 TRACE 94537 --- [           main] o.s.c.e.PropertySourcesPropertyResolver  : Searching for key 'feature.foo' in [systemEnvironment]
2018-02-17 20:30:24.206 TRACE 94537 --- [           main] o.s.c.e.PropertySourcesPropertyResolver  : Searching for key 'feature.foo' in [random]
2018-02-17 20:30:24.206 TRACE 94537 --- [           main] o.s.c.e.PropertySourcesPropertyResolver  : Searching for key 'feature.foo' in [applicationConfigurationProperties]
2018-02-17 20:30:24.206 DEBUG 94537 --- [           main] o.s.c.e.PropertySourcesPropertyResolver  : Could not find key 'feature.foo' in any property source
2018-02-17 20:30:24.210 TRACE 94537 --- [           main] o.s.c.a.ClassPathBeanDefinitionScanner   : Ignored because not matching any filter: file [/Users/karthick/feature-toggles/target/classes/in/karthick/spring/web/FooController.class]

However, if I use the same @Toggle annotation on a method inside rest controller class, the handler mapping finds the right method to invoke.

@RequestMapping("/foo")
  @Toggle(feature = "feature.foo")
  public Map hello() {
    return Collections.singletonMap("message", "hello foo!");
  }

How can I make sure spring boot maps to right handler method even if I use custom annotation on top of rest controller.

  • This problem remembers me a lot the problem using `@PreAuthorize` on Spring Controllers. See this links: [this](https://stackoverflow.com/a/11347824/2387977), [this](https://stackoverflow.com/a/32443631/2387977) and [this](https://stackoverflow.com/a/27080921/2387977) – Dherik Feb 17 '18 at 15:26

0 Answers0