SpringMvc-@ControllerAdvice

用途
因为@InitBinder和@ModelAttribute是在Controller中使用的,如果每个Controller都使用,这样就需要继承一个BaseController,但是假如有的Controller已经继承了别的类,所以这种方法就不太好用了,此时使用@ControllerAdvice配和上述两个注解最完美不过了,@ControllerAdvice包含了五个参数,可以自定义哪些controller需要initbinder和modelattribute,哪些又不需要,非常灵活

用法
@ControllerAdvice
public 你的类 implements ResponseBodyAdvice接口
注意,刚开始我以为用@Company或者@Configration等注入bean的注解就可以呢,其实不是,必须实现接口,并且注解必须是@ControllerAdvice才行

猜你喜欢

转载自blog.csdn.net/u011624903/article/details/80253514
今日推荐