集中记录spring常见注解 供今后查阅
@ControllerAdvice:
ControllerAdvice的定义为:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface ControllerAdvice
ControllerAdvice注解的作用是将@ExceptionHandler、@InitBinder、@ModelAttribute应用到到所有的requestmapping注解中去 (转自开涛的博客)
@ExceptionHandler
当controller中任意一个方法出现该异常时一定会被该方法给拦截 如:
@ExceptionHandler(UnauthorizedException.class)
@ResponseStatus(HttpStatus.UNAUTHORIZED)
public ModelAndView processUnauthenticatedException(NativeWebRequest request,
UnauthorizedException e){
ModelAndView mv = new ModelAndView();
mv.addObject("exception", e);
mv.setViewName("unauthorized");
return mv;
}