赞
踩
1.添加全局异常处理类
package com.iflytek.edu.hnezzhxy.common.base; import com.iflytek.edu.hnezzhxy.common.enums.ResponseCodeEnum; import com.iflytek.edu.hnezzhxy.util.ResponseResultUtil; import com.iflytek.edu.hnezzhxy.vo.ResultVO; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.validation.ObjectError; import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.MissingServletRequestParameterException; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; import javax.validation.ConstraintViolation; import javax.validation.ConstraintViolationException; import java.util.ArrayList; import java.util.List; import java.util.Set; /** * @description 全局异常处理类 * @create 2020/06/18 15:44 * @version 1.0 */ @RestControllerAdvice public class GlobalExceptionHandler { protected Logger logger = LoggerFactory.getLogger(getClass()); /** * @description 处理所有不可知的异常 * @param e 异常 * @return ResultVO */ /** * @description 处理所有不可知的异常 * @param e 异常 * @return ResultVO */ @ExceptionHandler({Exception.class}) public ResultVO globalExceptionHandler(Exception e) { this.logger.error(e.getMessage(), e); return new ResultVO(ResponseCodeEnum.OPERATE_FAIL.getCode(), ResponseCodeEnum.OPERATE_FAIL.getMessage(), null, false); } /** * @description 处理所有的参数缺少异常 * @param e 异常 * @return ResultVO */ @ExceptionHandler(MissingServletRequestParameterException.class) public ResultVO missingServletRequestParameter(MissingServletRequestParameterException e) { String parameterName = e.getParameterName(); String parameterType = e.getParameterType(); StringBuilder sb=new StringBuilder(); String message=sb.append(parameterName).append("必须要传!并且类型为").append(parameterType).toString(); return new ResultVO(400,message,null,false); }
例子:
/** * @description 对前端传到后端的对象封装的参数进行验证异常 * @param e 异常 * @return ResultVO */ @ExceptionHandler({MethodArgumentNotValidException .class}) //申明捕获那个异常类 public List<ResultVO> methodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e) { List<ResultVO> list=new ArrayList<>(); List<ObjectError> allErrors = e.getBindingResult().getAllErrors(); if(allErrors!=null&&allErrors.size()>0){ for (ObjectError o:allErrors) { this.logger.error(o.getDefaultMessage(), o); list.add(new ResponseResultUtil().error(400,o.getDefaultMessage())); } } return list; }
例子:对@Valid @RequestBody MyEntrollDto 中的属性进行验证
/** * 对控制层的单个定义的一些属性进行验证异常处理 * @date 2020-06-16 * @param e * @return */ @ExceptionHandler({ConstraintViolationException.class}) //申明捕获那个异常类 public List<ResultVO> constraintViolationExceptionHandler(ConstraintViolationException e) { List<ResultVO> list=new ArrayList<>(); Set<ConstraintViolation<?>> set = e.getConstraintViolations(); for(ConstraintViolation c:set){ this.logger.error(c.getMessage(), c); list.add(new ResponseResultUtil().error(400,c.getMessage())); } return list; }
例子:对控制层的参数@NotBlank(message = “学生sid不能为空!”) @RequestParam(value=“sid”)验证
}
2.响应码枚举类
package com.iflytek.edu.hnezzhxy.common.enums; /** * @description 请求响应状态码和提示信息枚举类 * @create 2020/06/18 15:44 * @version 1.0 */ public enum ResponseCodeEnum { /** 请求成功状态码和提示信息 **/ SUCCESS(200, "success"), /** 尚未登录 **/ UNLOGIN_ERROR(0, "尚未登录!"), /** 登陆成功 **/ LOGIN_SUCCESS(1,"登陆成功!"), /** 注册成功 **/ REGISTER_SUCCESS(2,"注册成功!"), /** 验证码过期 **/ VALIDATE_EXPIRE(3,"验证码过期!"), /** 用户密码修改成功 **/ VALIDATE_WRITER_ERROR(4,"验证码填写错误!"), /** 用户密码修改成功 **/ USERPASS_UPDATE_SUCCESS(5,"用户密码修改成功!"), /** 手机号已经被注册 **/ HAS_REGISTER(6,"手机号已经被注册!"), /** 手机号发送验证码成功 **/ PHONE_SEND_SUCCESS(7,"手机验证码发送成功!"), /** 登陆成功 **/ LOGINOUT_SUCCESS(8,"退出成功!"), /** 登陆成功 **/ IMPORT_SUCCESS(9,"导入成功!"), /** 登陆成功 **/ IMPORT_ERROR(10,"导入失败!"), /** 程序报错响应信息 **/ OPERATE_FAIL(500, "error"), /** 短信云平台响应失败 **/ DX_CLOUD_fAIL(501, "短信云平台响应失败!请联系相关负责人!"), /** 该手机号还未被注册,不能找回! **/ PHONE_NO_REGISTER(502, "该手机号还未被注册,不能找回!"), /** AES解密失败 **/ AES_DEC_FAIL(503, "AES解密失败!请验证解密规则!"); private Integer code; private String message; ResponseCodeEnum(Integer code, String message) { this.code = code; this.message = message; } public final Integer getCode() { return this.code; } public final String getMessage() { return this.message; } }
3.ResultVO
package com.iflytek.edu.hnezzhxy.vo; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @AllArgsConstructor @NoArgsConstructor @ApiModel(value = "ResultVO",description = "统一返回的对象") public class ResultVO<T> { /** 响应码 **/ @ApiModelProperty(value = "响应码") private Integer code; /** 响应消息 **/ @ApiModelProperty(value = "响应消息") private String message; /** 数据 **/ @ApiModelProperty(value = "数据信息") private Object data; /** 响应标识 **/ @ApiModelProperty(value = "响应标识") private Boolean success; }
4.PageVO
package com.iflytek.edu.hnezzhxy.vo; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.AllArgsConstructor; import lombok.Data; import java.util.List; @Data @AllArgsConstructor @ApiModel(value = "PageVO",description = "分页信息对象") public class PageVO<T> { /** 封装得数据 **/ @ApiModelProperty(value = "封装得数据") private List<T> datalist; /** 封装数据得分页数据 **/ @ApiModelProperty(value = "分页信息数据") private Page page; }
5.Page
package com.iflytek.edu.hnezzhxy.vo; import lombok.AllArgsConstructor; import lombok.Data; /** * 分页信息 */ @Data @AllArgsConstructor public class Page { /** 当前页 **/ private Integer currentPage; /** 需要总数 **/ private Boolean needTotal; /** 分页大小 **/ private Integer pageSize; /** 总数 **/ private Integer totalCount; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。