赞
踩
对于@ControllerAdvice,结合@ExceptionHandler用于全局异常的处理。 @ExceptionHandler的作用主要在于声明一个或多个类型的异常,当符合条件的Controller抛出这些异常之后将会对这些异常进行捕获,然后按照其标注的方法的逻辑进行处理,从而改变返回的视图信息。
- package com.tenq.exception;
-
-
- import lombok.Getter;
-
- /**
- */
- @Getter
- public enum ExceptionEnum {
- INVALID_FILE_TYPE(400, "无效的文件类型!"),
- INVALID_PARAM_ERROR(400, "无效的请求参数!"),
- INVALID_PHONE_NUMBER(400, "无效的手机号码"),
- INVALID_VERIFY_CODE(400, "验证码错误!"),
- INVALID_USERNAME_PASSWORD(400, "无效的用户名和密码!"),
- INVALID_SERVER_ID_SECRET(400, "无效的服务id和密钥!"),
- INVALID_NOTIFY_PARAM(400, "回调参数有误!"),
- INVALID_NOTIFY_SIGN(400, "回调签名有误!"),
-
- CATEGORY_NOT_FOUND(404, "商品分类不存在!"),
- BRAND_NOT_FOUND(404, "品牌不存在!"),
- SPEC_NOT_FOUND(404, "规格不存在!"),
- GOODS_NOT_FOUND(404, "商品不存在!"),
- CARTS_NOT_FOUND(404, "购物车不存在!"),
- APPLICATION_NOT_FOUND(404, "应用不存在!"),
- ORDER_NOT_FOUND(404, "订单不存在!"),
- ORDER_DETAIL_NOT_FOUND(404, "订单数据不存在!"),
-
- DATA_TRANSFER_ERROR(500, "数据转换异常!"),
- INSERT_OPERATION_FAIL(500, "新增操作失败!"),
- UPDATE_OPERATION_FAIL(500, "更新操作失败!"),
- DELETE_OPERATION_FAIL(500, "删除操作失败!"),
- FILE_UPLOAD_ERROR(500, "文件上传失败!"),
- DIRECTORY_WRITER_ERROR(500, "目录写入失败!"),
- FILE_WRITER_ERROR(500, "文件写入失败!"),
- SEND_MESSAGE_ERROR(500, "短信发送失败!"),
- INVALID_ORDER_STATUS(500, "订单状态不正确!"),
- STOCK_NOT_ENOUGH_ERROR(500, "库存不足!"),
- NUM_BAD( 501,"数据异常!"),
- UNAUTHORIZED(401, "登录失效或未登录!");
-
- private int status;
- private String message;
-
- //枚举对象中的构造方法不能为public,不能被外部调用
- ExceptionEnum(int status, String message) {
- this.status = status;
- this.message = message;
- }
- }
- package com.tenq.exception;
-
- import lombok.Getter;
-
- /**
- * 自定义异常对象,来定义异常状态码
- */
- @Getter
- public class CommonRuntimeException extends RuntimeException{
- private Integer status;
-
- /**
- * 自己临时自定义状态码和状态信息
- * @param status 状态
- * @param message 状态信息
- */
- public CommonRuntimeException(Integer status, String message) {
- super(message);
- this.status = status;
- }
-
- /**
- *
- * @param exceptionEnum 从枚举对象中获取状态码和状态信息
- */
- public CommonRuntimeException(ExceptionEnum exceptionEnum) {
- super(exceptionEnum.getMessage());
- this.status = exceptionEnum.getStatus();
- }
-
- }
- package com.tenq.advice;
-
-
- import com.tenq.entity.Result;
- import com.tenq.exception.CommonRuntimeException;
- import org.springframework.web.bind.annotation.ControllerAdvice;
- import org.springframework.web.bind.annotation.ExceptionHandler;
- import org.springframework.web.bind.annotation.ResponseBody;
-
- @ControllerAdvice
- public class ExceptionHandlerController {
- /**
- * ExceptionHandler(CommonRuntimeException.class)
- * 表示当前处理器只处理CommonRuntimeException异常
- * @return
- */
- @ExceptionHandler(CommonRuntimeException.class)
- @ResponseBody
- public Result handlerException(CommonRuntimeException e){
- //异常返回false,Result是上一篇接口返回对象。
- return new Result(false, e.getStatus(),e.getMessage());
- }
-
- }
1.自定义状态码和状态信息
- @GetMapping("/test")
- // @ResponseEncrypt
- public Result test() {
- int a;
- try {
- a=1/0;
- }catch (Exception e){
- //自定义状态码和状态信息
- throw new CommonRuntimeException(0001,"运算规则错误");
- }
- return new Result(true, StatusCode.OK, "成功");
- }
2.使用异常枚举信息分类
- @GetMapping("/test")
- // @ResponseEncrypt
- public Result test() {
- int a;
- try {
- a=1/0;
- }catch (Exception e){
- //使用异常枚举信息分类
- throw new CommonRuntimeException(ExceptionEnum.NUM_BAD);
- }
- return new Result(true, StatusCode.OK, "成功");
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。