Spring Boot:在spring rest控制器类上有一个自定义注释,导致识别处理程序映射时出错

时间:2018-02-17 13:31:01

标签: spring spring-mvc spring-boot annotations

我在Spring启动时在rest控制器类的顶部使用一个名为@Toggle的自定义注释来拦截handlerType以阻止其余的调用。

见下面的代码片段:

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

但是,即使在调度程序servlet找到处理程序方法之前,我也无法访问失败的api。

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

在下面的日志中找到了可以更好地解决问题的日志。

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]

但是,如果我对rest控制器类中的方法使用相同的@Toggle注释,则处理程序映射会找到要调用的正确方法。

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

即使我在rest控制器上使用自定义注释,如何确保spring boot映射到正确的处理程序方法。

0 个答案:

没有答案