自定义方法级别的注解,在接口上使用,配合拦截器,可以在指定接口上添加自定义注解类,在拦截器中获取自定义注解对象,来控制是否继续执行逻辑。(当然这里:只用拦截器也完全可以实现,这里只是mark一下这种方式)
- 自定义注解类
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface FilterPoint {
String value() default "";
String status() default "1";
}
- 接口层添加自定义注解
@RequestMapping("/register")
@ApiOperation(value = "参数校验v2.0", notes = "注册校验", httpMethod = "GET", response = Map.class)
@ApiImplicitParams({
@ApiImplicitParam(name = "name", value = "用户名"),
@ApiImplicitParam(name = "mobile", value = "手机号"),
@ApiImplicitParam(name = "verifyCode", value = "验证码"),
})
@FilterPoint(value = "注册参数校验")
public String register() throws NewHouseException {
}
- 拦截器中获取自定义的注解对象
public class FilterPointInterceptor extends HandlerInterceptorAdapter {
@Resource
private ConfigService configService;
final String isFilter = "IS_FILTER";
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
WordFilterPoint annotation = null;
if(handler instanceof HandlerMethod) {
annotation = ((HandlerMethod) handler).getMethodAnnotation(FilterPoint.class);
}
String flag = configService.get(isFilter);
if (annotation != null && "Y".equalsIgnoreCase(flag)) {
}
return true;
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
}
@Override
public void afterConcurrentHandlingStarted(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
}