远方蔚蓝
一刹那情真,相逢不如不见

文章数量 126

访问次数 199896

运行天数 1437

最近活跃 2024-10-04 23:36:48

进入后台管理系统

SpringMVC中@ControllerAdvice注解的三种使用场景


@ControllerAdvice ,很多初学者可能都没有听说过这个注解,实际上,这是一个非常有用的注解,顾名思义,这是一个增强的 Controller。使用这个 Controller ,可以实现三个方面的功能:
  1. 全局异常处理
  1. 全局数据绑定
  1. 全局数据预处理
灵活使用这三个功能,可以帮助我们简化很多工作,需要注意的是,这是 SpringMVC 提供的功能,在 Spring Boot 中可以直接使用,下面分别来看。
全局异常处理
使用 @ControllerAdvice 实现全局异常处理,只需要定义类,添加该注解即可定义方式如下:
@ControllerAdvice
public class MyGlobalExceptionHandler {
    @ExceptionHandler(Exception.class)
    public ModelAndView customException(Exception e) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("message", e.getMessage());
        mv.setViewName("myerror");
        return mv;
    }
}
在该类中,可以定义多个方法,不同的方法处理不同的异常,例如专门处理空指针的方法、专门处理数组越界的方法...,也可以直接向上面代码一样,在一个方法中处理所有的异常信息。
@ExceptionHandler 注解用来指明异常的处理类型,即如果这里指定为 NullpointerException,则数组越界异常就不会进到这个方法中来。
以下为另外一个完整的例子
import javax.validation.ConstraintViolationException;
import org.apache.shiro.authz.UnauthorizedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import wst.st.site.clazz.response.ServerResponse;
import wst.st.site.constant.enums.ResponseCodeEnum;
import wst.st.site.constant.message.SiteResponseMessage;
import wst.st.site.exception.SiteCustomException;
import wst.st.site.exception.SiteShiroAuthenticationException;
/**
 * 统一异常处理 // 必须放在spring可以扫描的地方
 * 
 * @author wst 20181024日 下午5:10:37
 *
 */
/*
 * @ControllerAdvice是一个@Component
 * 用于定义@ExceptionHandler@InitBinder@ModelAttribute方法,
 * 适用于所有使用@RequestMapping方法。 Sping3之后使用
 */
@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {
	private static final Logger log = LoggerFactory.getLogger(SiteCustomException.class);
	
	/**
	 * 500 - Internal Server Error
	 * @author wst 20181024日 下午5:12:00
	 * @param e
	 * @return
	 */
	/*
	 * @ResponseStatus 响应给前端的状态
	 * @ExceptionHandler 如果只使用@ExceptionHandler
	 * 只能在当前Controller中处理异常。 
	 * 如果在@ControllerAdvice中使用,可以在任何地方配置,无需再在Controller使用@ExceptionHandler
	 */
	@ResponseStatus(HttpStatus.OK)
	@ExceptionHandler(value = {SiteCustomException.class, SiteShiroAuthenticationException.class, RuntimeException.class, Exception.class})
	public ServerResponse<String> businessException(Exception e) {
		String msg = "请求错误";
		if (e instanceof SiteCustomException) {
			/*
			 * 自定义异常返回自定义的错误信息
			 */
			msg = ((SiteCustomException) e).getMessage();
			log.error("捕获异常 -------->SiteCustomException: e={}", e.getMessage());
		} else if (e instanceof SiteShiroAuthenticationException) {
			/*
			 * 自定义异常返回自定义的错误信息
			 */
			msg = ((SiteShiroAuthenticationException) e).getMessage();
			log.error("捕获异常 -------->SiteShiroAuthenticationException: e={}", e.getMessage());
		}  else if (e instanceof UnauthorizedException) {
			/*
			 * 自定义异常返回自定义的错误信息
			 */
//			msg = ((UnauthorizedException) e).getMessage();
			msg = SiteResponseMessage.Auth.SUBJECT_NOT_PERMISSION;
			log.error("捕获异常 -------->UnauthorizedException: e={}", e.getMessage());
		} else {
			/**
			 * 其他一切异常统一返回
			 */
			msg = SiteResponseMessage.Common.SERVER_RESPONSE_FAILED;
			log.error("捕获异常 -------->Exception: e={}", e.getMessage());
		}
		e.printStackTrace();
		// 把错误信息返回给前端
		return ServerResponse.createByErrorMessage(msg);
	}
	/**
	 * 400 - Bad Request
	 */
	@ResponseStatus(HttpStatus.BAD_REQUEST)
	@ExceptionHandler({ HttpMessageNotReadableException.class, MissingServletRequestParameterException.class,
			BindException.class, ServletRequestBindingException.class, MethodArgumentNotValidException.class,
			ConstraintViolationException.class })
	public ServerResponse<String> handleHttpMessageNotReadableException(Exception e) {
		log.error("参数解析失败", e);
//		if (e instanceof BindException) {
//			return ServerResponse.createByErrorMessage(ResponseCodeEnum.BAD_REQUEST.getDesc() + " - "
//					+ ((BindException) e).getAllErrors().get(0).getDefaultMessage());
//		}
		return ServerResponse.createByErrorMessage(ResponseCodeEnum.BAD_REQUEST.getDesc());
	}
	/**
	 * 405 - Method Not Allowed
	 */
	@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
	@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
	public ServerResponse<String> handleHttpRequestMethodNotSupportedException(
			HttpRequestMethodNotSupportedException e) {
		log.error("不支持当前请求方法", e);
		return ServerResponse.createByErrorMessage(ResponseCodeEnum.METHOD_NOT_ALLOWED.getDesc());
	}
}
全局数据绑定
全局数据绑定功能可以用来做一些初始化的数据操作,我们可以将一些公共的数据定义在添加了 @ControllerAdvice 注解的类中,这样,在每一个 Controller 的接口中,就都能够访问导致这些数据。
使用步骤,首先定义全局数据,如下:
@ControllerAdvice
public class MyGlobalExceptionHandler {
    @ModelAttribute(name = "md")
    public Map<String,Object> mydata() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("age", 99);
        map.put("gender", "男");
        return map;
    }
}
使用 @ModelAttribute 注解标记该方法的返回数据是一个全局数据,默认情况下,这个全局数据的 key 就是返回的变量名,value 就是方法返回值,当然开发者可以通过 @ModelAttribute 注解的 name 属性去重新指定 key。
定义完成后,在任何一个Controller 的接口中,都可以获取到这里定义的数据:
@RestController
public class HelloController {
    @GetMapping("/hello")
    public String hello(Model model) {
        Map<String, Object> map = model.asMap();
        System.out.println(map);
        int i = 1 / 0;
        return "hello controller advice";
    }
}
全局数据预处理
考虑我有两个实体类,Book 和 Author,分别定义如下:
public class Book {
    private String name;
    private Long price;
    //getter/setter
}
public class Author {
    private String name;
    private Integer age;
    //getter/setter
}
此时,如果我定义一个数据添加接口,如下:
@PostMapping("/book")
public void addBook(Book book, Author author) {
    System.out.println(book);
    System.out.println(author);
}
这个时候,添加操作就会有问题,因为两个实体类都有一个 name 属性,从前端传递时 ,无法区分。此时,通过 @ControllerAdvice 的全局数据预处理可以解决这个问题
解决步骤如下:
1.给接口中的变量取别名
@PostMapping("/book")
public void addBook(@ModelAttribute("b") Book book, @ModelAttribute("a") Author author) {
    System.out.println(book);
    System.out.println(author);
}
2.进行请求数据预处理
在 @ControllerAdvice 标记的类中添加如下代码:
@InitBinder("b")
public void b(WebDataBinder binder) {
    binder.setFieldDefaultPrefix("b.");
}
@InitBinder("a")
public void a(WebDataBinder binder) {
    binder.setFieldDefaultPrefix("a.");
}
@InitBinder("b") 注解表示该方法用来处理和Book和相关的参数,在方法中,给参数添加一个 b 前缀,即请求参数要有b前缀.
3.发送请求
请求发送时,通过给不同对象的参数添加不同的前缀,可以实现参数的区分.
a.name=""
a.age=""
b.name=""
b.prince=""