通常在开发具体项目过程中我们可能会面临如下问题:
在spring中的处理方式是使用
@RestControllerAdvice public class CommonResponseDataAdvice implements ResponseBodyAdvice<Object> { @Override @SuppressWarnings("all") public boolean supports(MethodParameter methodParameter, Class<? extends HttpMessageConverter<?>> aClass) { if (methodParameter.getDeclaringClass().isAnnotationPresent( IgnoreResponseAdvice.class )) { return false; } if (methodParameter.getMethod().isAnnotationPresent( IgnoreResponseAdvice.class )) { return false; } return true; } @Nullable @Override @SuppressWarnings("all") public Object beforeBodyWrite(@Nullable Object o, MethodParameter methodParameter, MediaType mediaType, Class<? extends HttpMessageConverter<?>> aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) { CommonResponse<Object> response = new CommonResponse<>(0, ""); if (null == o) { return response; } else if (o instanceof CommonResponse) { response = (CommonResponse<Object>) o; } else { response.setData(o); } return response; } }
上述代码中定义了一个注解IgnoreResponseAdvice,如果controller的类或者方法有这个注解就不做处理。下面这个例子展现的是如何在controller抛出异常的时候,自动包装成为commonRespons。
@RestControllerAdvice public class GlobalExceptionAdvice { @ExceptionHandler(value = ParamException.class) public CommonResponse<String> handlerParamException(HttpServletRequest req, ParamException ex) { CommonResponse<String> response = new CommonResponse<>(400, "param error"); response.setData(ex.getMessage()); return response; } @ExceptionHandler(value = BusinessException.class) public CommonResponse<String> handlerBusinessException(HttpServletRequest req, BusinessException ex) { CommonResponse<String> response = new CommonResponse<>(500, "business error"); response.setData(ex.getMessage()); return response; } @ExceptionHandler(value = SystemException.class) public CommonResponse<String> handlerSystemException(HttpServletRequest req, SystemException ex) { CommonResponse<String> response = new CommonResponse<>(700, "system error"); response.setData(ex.getMessage()); return response; } }
对比下面的controller能更清楚的明白如何使用。
@RestController public class IndexController { @RequestMapping("/") String home() { return "Hello World!"; } @IgnoreResponseAdvice @RequestMapping("/hi") String hi() { return "Hello World!"; } @RequestMapping("/param") String param() throws Exception { throw new ParamException("参数错误"); } @RequestMapping("/business") String business() throws Exception { throw new BusinessException("业务错误"); } @RequestMapping("/system") String system() throws Exception { throw new SystemException("系统错误"); } }
详细的代码见 https://gitee.com/dongqihust/arst/tree/master/custom-response
特别的,这个文章提供了几种其他的解决方案:https://www.baeldung.com/exception-handling-for-rest-with-spring
ARTS打卡计划第一周-Tips-ControllerAdvice的使用
原文:https://www.cnblogs.com/dongqiSilent/p/10742803.html