【发布时间】:2018-01-05 12:04:45
【问题描述】:
@Controller 类中的各种@RequestMapping 方法具有我想在运行时分析的自定义注释。
例如:
@Controller
@RequestMapping("/bla")
@RequireCommunityLevel("...")
@AnotherCustomAnnotation(value = "...")
public class FakeController {
// ...
@RequireScore(level = Score.Platinum, score = 8500)
@RequestMapping("/blabla")
@AnotherCustomAnnotation(value = "...")
public DTOResponse<MySpecialModel> getMySpecialModels() {
// ...
}
}
当我在自定义 Interceptor 和 Filter 类中收到请求时,我想分析该请求将调用哪个 Method。
我过去曾使用基于Javax Servlet 和Jetty 的自定义框架,并且经常实现某种可以查询的方法注册表。
例如:
public class CustomFilter extends Filter {
@Inject
private MyMethodRegistry registry;
@Override
public void doFilter(
ServletRequest request,
ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// get the request path
final String path = getPath(request);
final Method m = this.registry.getMethodForPath(path);
// that's what I mean to do
if (m.getAnnotation(RequireScore.class) != null) { ... }
if (m.getDeclaringClass().getAnnotation(AnotherCustomAnnotation.class != null) { ... }
chain.doFilter(request, response);
}
}
我正在Spring 中寻找类似的方法,可能是一个内置实用程序,可以在运行时分析我的@RequestMapping 方法上的Annotation。所有这些都应该发生在我的拦截器/过滤器中。
【问题讨论】:
-
如果您使用
HandlerInterceptor,您已经拥有所需的处理程序。 -
@M.Deinum 好吧,你的意思是
handler对象在preHandle中作为参数传递? -
那个是的。
标签: java spring spring-mvc reflection annotations