@RestContrllerAdvice是一种组合注解,由@ControllerAdvice,@ResponseBody组成
@ControllerAdvice继承了@Component,反过来,可以理解为@RestContrllerAdvice本质上就是@Component
本质上是一个类,泛指各种组件,就是说当我们的类不属于各种归类的时候(不属于@Controller,@Service等的时候),我们就可以使用@Component
作用就是实现bean注入,利用这个注解可以取代spring的xml配置文件
com.alibaba fastjson1.2.75
@RestController public class TestController { @Autowired private TestService testService; @GetMapping("/test3") public String test3() { return testService.test(); } }
public interface TestService { String test(); }
@Component public class TestServiceImpl implements TestService { @Autowired private ObjectMapper objectMapper; @Override public String test() { String b = null; try { b = objectMapper.writeValueAsString("a"); System.out.println(b); } catch (Exception e) { } return b; } }
自定义客户端返回格式
捕获客户端返回异常
捕获客户端返回异常案例(自定义返回异常)
public interface BaseCodeMsg { String getCode(); String getMsg(); }
public enum PlatformExceptionMsgEnum implements BaseCodeMsg { /** * base平台返回的异常信息 */ SELECT_NULL("000000001", "查询数据为空"), INVOKE_IS_REJECT("00000002", "请求被拒绝"), ILLEGAL_ARGUMENT_FORMAT("000000003", "非法参数格式"), ILLEGAL_ARGUMENT("000000004","数据非法"), SYSTEM_EXCEPTION("000000005", "系统异常"); /** * 错误码 */ private final String code; /** * 错误信息 */ private final String msg; PlatformExceptionMsgEnum(String code, String msg) { this.code = code; this.msg = msg; } @Override public String getCode() { return code; } @Override public String getMsg() { return msg; } }
public abstract class AbstractApiException extends RuntimeException{ public AbstractApiException() { } public abstract String getCode(); public abstract String getMsg(); }
public class PlatformException extends AbstractApiException { private String code; private String msg; public PlatformException(BaseCodeMsg baseCodeMsg) { code = baseCodeMsg.getCode(); msg = baseCodeMsg.getMsg(); } public static void throwException(BaseCodeMsg baseCodeMsg) { throw new PlatformException(baseCodeMsg.getCode(), baseCodeMsg.getMsg()); } public static void throwException(BaseCodeMsg baseCodeMsg, String msg) { throw new PlatformException(baseCodeMsg.getCode(), StringUtils.isEmpty(baseCodeMsg.getMsg()) ? msg : baseCodeMsg.getMsg()); } public static void throwException(String code, String msg) { throw new PlatformException(code, msg); } public static void throwException(String msg) { throw new PlatformException(PlatformExceptionMsgEnum.SYSTEM_EXCEPTION.getCode(), msg); } public PlatformException(String code, String msg) { this.code = code; this.msg = msg; } @Override public String getCode() { return code; } @Override public String getMsg() { return msg; } }
@Data public class BaseResultimplements Serializable { private static final long serialVersionUID = 1L; private String code; private String msg; private String traceId; private T data; public boolean isSuccess() { return PlatformExceptionMsgEnum.SUCCESS.getCode().equals(this.code); } public static BaseResult ok() { return restBaseResult((Object)null, PlatformExceptionMsgEnum.SUCCESS); } public static BaseResult ok(T data) { return restBaseResult(data, PlatformExceptionMsgEnum.SUCCESS); } public static BaseResult fail() { return restBaseResult((Object)null, PlatformExceptionMsgEnum.SYSTEM_ERROR); } public static BaseResult fail(BaseCodeMsg baseCodeMsg) { return restBaseResult((Object)null, baseCodeMsg); } public static BaseResult fail(String code, String msg) { return restBaseResult((Object)null, code, msg); } private static BaseResult restBaseResult(T data, BaseCodeMsg baseCodeMsg) { return restBaseResult(data, baseCodeMsg.getCode(), baseCodeMsg.getMsg()); } private static BaseResult restBaseResult(T data, String code, String msg) { BaseResult apiBaseResult = new BaseResult(); apiBaseResult.setCode(code); apiBaseResult.setData(data); apiBaseResult.setMsg(msg); apiBaseResult.setTraceId(MDC.get("traceId")); return apiBaseResult; } public BaseResult() { } }
@RestController public class TestController { @GetMapping("/test") public void test() { PlatformException.throwException(PlatformExceptionMsgEnum.SYSTEM_EXCEPTION); } }
@RestControllerAdvice @Configuration public class GlobalExceptionHandler { private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class); private MessageSource messageSource; /** * 捕获全局异常类 * * @param e * @return */ @ExceptionHandler({MethodArgumentNotValidException.class}) public BaseResult handle(MethodArgumentNotValidException e) { if (e.getBindingResult().hasErrors()) { String msg = ((ObjectError) e.getBindingResult().getAllErrors().get(0)).getDefaultMessage(); return BaseResult.fail(PlatformExceptionMsgEnum.ILLEGAL_ARGUMENT.getCode(), this.getI18nMsg(msg)); } else { log.info(e.getMessage(), e); return BaseResult.fail(PlatformExceptionMsgEnum.ILLEGAL_ARGUMENT.getCode(), this.getI18nMsg(PlatformExceptionMsgEnum.ILLEGAL_ARGUMENT.getMsg())); } } /** * 捕获API调用接口的异常类 * * @param e * @return */ @ExceptionHandler(AbstractApiException.class) public BaseResult abstractApiException(AbstractApiException e) { return BaseResult.fail(e.getCode(), e.getMsg()); } /** * 前端传入的参数和后端传入的参数接收不匹配 * * @param e * @return */ @ExceptionHandler({HttpMessageNotReadableException.class}) public BaseResult handle(HttpMessageNotReadableException e) { log.warn(e.getMessage(), e); return BaseResult.fail(PlatformExceptionMsgEnum.ILLEGAL_ARGUMENT_FORMAT.getCode(), this.getI18nMsg(PlatformExceptionMsgEnum.ILLEGAL_ARGUMENT_FORMAT.getMsg())); } /** * 子类异常(相当于子类不能抛出比父类更广泛异常) * * @param e * @return */ @ExceptionHandler({UndeclaredThrowableException.class}) public BaseResult handle(UndeclaredThrowableException e) { log.warn(e.getMessage(), e); return BaseResult.fail(PlatformExceptionMsgEnum.INVOKE_IS_REJECT.getCode(), this.getI18nMsg(PlatformExceptionMsgEnum.INVOKE_IS_REJECT.getMsg())); } /** * 异常捕获 * * @param e 捕获的异常,封装返回的对象 * @return */ @ExceptionHandler(Exception.class) public BaseResult handleException(Exception e) { log.warn(e.getMessage(), e); StringJoiner joiner = new StringJoiner(":"); joiner.add(this.getI18nMsg(PlatformExceptionMsgEnum.INVOKE_IS_REJECT.getMsg())).add(e.getMessage()); return BaseResult.fail(PlatformExceptionMsgEnum.SYSTEM_EXCEPTION.getCode(), joiner.toString()); } private String getI18nMsg(String msg) { try { return this.messageSource.getMessage(msg, (Object[]) null, LocaleContextHolder.getLocale()); } catch (NoSuchMessageException var3) { return msg; } } public GlobalExceptionHandler(MessageSource messageSource) { this.messageSource = messageSource; } }
没有自定义返回结果之前
自定义返回结果以后
@RestController public class TestController { @GetMapping("/test2") public String test2() { return "test2"; } }
BaseResult返回结果类在上面,对应需要导入json的jar也在上面
@RestControllerAdvice( annotations = {RestController.class} ) @Configuration public class BaseResultResponseAdvice implements ResponseBodyAdvice
@RestControllerAdvice的使用还可以指定对应的注解、包,类。比如你需要返回自定义结果格式,可以指定@RestController层使用,如果你要指定类和包也可以
@RestControllerAdvice( annotations = {RestController.class} )
@RestControllerAdvice( basePackageClasses = TestController.class )
@RestControllerAdvice( basePackages = "com.common.base.controller" )