一、请求参数加校验

  1. package com.atguigu.eduservice.entity.reqVo;
  2. import com.atguigu.common.valid.ListValue;
  3. import io.swagger.annotations.ApiModelProperty;
  4. import lombok.Data;
  5. import javax.validation.constraints.Min;
  6. import javax.validation.constraints.NotBlank;
  7. import javax.validation.constraints.Size;
  8. import java.io.Serializable;
  9. @Data
  10. public class TeacherVo implements Serializable {
  11. private static final long serialVersionUID = 1L;
  12. @ApiModelProperty(value = "讲师姓名")
  13. @NotBlank(message = "讲师姓名必须提交")
  14. @Size(min = 1, max = 6)
  15. private String name;
  16. @ApiModelProperty(value = "讲师简介")
  17. @NotBlank(message = "讲师简介必须提交")
  18. private String intro;
  19. @ApiModelProperty(value = "讲师资历,一句话说明讲师")
  20. private String career;
  21. @ApiModelProperty(value = "头衔 1高级讲师 2首席讲师")
  22. @ListValue(vals={1,2},message = "只能输入1或者2")
  23. private Integer level;
  24. @ApiModelProperty(value = "讲师头像")
  25. private String avatar;
  26. @ApiModelProperty(value = "排序")
  27. @Min(value = 0,message = "排序必须大于等于0")
  28. private Integer sort;
  29. }

二、controller的方法上加注解:@Validated

  1. /**
  2. * 新增讲师
  3. * @param teacherVo
  4. * @return
  5. */
  6. @PostMapping("addTeacher")
  7. public R addTeacher(@Validated @RequestBody TeacherVo teacherVo){
  8. EduTeacher eduTeacher = new EduTeacher();
  9. BeanUtils.copyProperties(teacherVo, eduTeacher);
  10. eduTeacher.setGmtCreate(new Date());
  11. eduTeacher.setGmtModified(new Date());
  12. eduTeacherService.save(eduTeacher);
  13. return R.ok();
  14. }

三、集中处理所有controller抛过来的异常

  1. package com.atguigu.eduservice.exception;
  2. /**
  3. * 集中处理所有controller抛过来的异常
  4. */
  5. @Slf4j
  6. //@ResponseBody
  7. //@ControllerAdvice(basePackages = "com.atguigu.eduservice.controller")
  8. @RestControllerAdvice(basePackages = "com.atguigu.eduservice.controller")
  9. public class ExceptionControllerAdvice {
  10. /**
  11. * 处理controller数据校验错误。精确处理异常
  12. * @param e
  13. * @return
  14. */
  15. @ExceptionHandler(value= MethodArgumentNotValidException.class)
  16. public R handleVaildException(MethodArgumentNotValidException e){
  17. log.error("数据校验出现问题{},异常类型:{}",e.getMessage(),e.getClass());
  18. BindingResult bindingResult = e.getBindingResult();
  19. Map<String,Object> errorMap = new HashMap<>();
  20. bindingResult.getFieldErrors().forEach((fieldError)->{
  21. // 获取到错误提示
  22. Object message = fieldError.getDefaultMessage();
  23. // 获取错误属性的名字
  24. String field = fieldError.getField();
  25. errorMap.put(field,message);
  26. });
  27. return R.error().data(errorMap);
  28. }
  29. /**
  30. * 处理自定义的异常
  31. * @param throwable
  32. * @return
  33. */
  34. @ExceptionHandler(value = GuliException.class)
  35. public R handleGuliException(GuliException e){
  36. log.error("错误:",e);
  37. return R.success(false).code(e.getCode()).message(e.getMsg());
  38. }
  39. /**
  40. * 处理所有类型的异常
  41. * @param throwable
  42. * @return
  43. */
  44. @ExceptionHandler(value = Throwable.class)
  45. public R handleException(Throwable throwable){
  46. log.error("错误:",throwable);
  47. return R.error();
  48. }
  49. }

四、新增讲师执行结果

图片.png

扩展:自定义校验注解

  1. @ApiModelProperty(value = "头衔 1高级讲师 2首席讲师")
  2. @ListValue(vals={1,2},message = "只能输入1或者2")
  3. private Integer level;
  1. package com.atguigu.common.valid;
  2. //ListValueConstraintValidator 自定义的校验器
  3. @Documented
  4. @Constraint(validatedBy = { ListValueConstraintValidator.class })
  5. @Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE })
  6. @Retention(RUNTIME)
  7. public @interface ListValue {
  8. // 在配置文件ValidationMessages.properties中
  9. String message() default "{com.atguigu.common.valid.ListValue.message}";
  10. Class<?>[] groups() default { };
  11. Class<? extends Payload>[] payload() default { };
  12. int[] vals() default { };
  13. }

自定义校验器

  1. package com.atguigu.common.valid;
  2. import javax.validation.ConstraintValidator;
  3. import javax.validation.ConstraintValidatorContext;
  4. import java.util.HashSet;
  5. import java.util.Set;
  6. public class ListValueConstraintValidator implements ConstraintValidator<ListValue,Integer> {
  7. private Set<Integer> set = new HashSet<>();
  8. //初始化方法
  9. @Override
  10. public void initialize(ListValue constraintAnnotation) {
  11. // 获取属性上得所有数据
  12. int[] vals = constraintAnnotation.vals();
  13. for (int val : vals) {
  14. set.add(val);
  15. }
  16. }
  17. /**
  18. * 判断是否校验成功
  19. * @param value 需要校验的值
  20. * @param context 整个上下文环境信息
  21. * @return 判断需要校验的值是否在属性上的所有枚举值
  22. */
  23. @Override
  24. public boolean isValid(Integer value, ConstraintValidatorContext context) {
  25. return set.contains(value);
  26. }
  27. }

五、自定义异常

  1. package com.atguigu.common.exceptionhandler;
  2. import lombok.AllArgsConstructor;
  3. import lombok.Data;
  4. import lombok.NoArgsConstructor;
  5. @Data
  6. @AllArgsConstructor //生成有参数构造方法
  7. @NoArgsConstructor //生成无参数构造
  8. public class GuliException extends RuntimeException {
  9. private Integer code; //状态码
  10. private String msg; //异常信息
  11. }

六、统一返回结果类

  1. package com.atguigu.common.utils;
  2. //统一返回结果的类
  3. @Data
  4. public class R {
  5. @ApiModelProperty(value = "是否成功")
  6. private Boolean success;
  7. @ApiModelProperty(value = "返回码")
  8. private Integer code;
  9. @ApiModelProperty(value = "返回消息")
  10. private String message;
  11. @ApiModelProperty(value = "返回数据")
  12. private Map<String, Object> data = new HashMap<String, Object>();
  13. //把构造方法私有
  14. private R() {}
  15. //成功静态方法
  16. public static R ok() {
  17. R r = new R();
  18. r.setSuccess(true);
  19. r.setCode(com.atguigu.common.utils.ResultCode.SUCCESS);
  20. r.setMessage("成功");
  21. return r;
  22. }
  23. //失败静态方法
  24. public static R error() {
  25. R r = new R();
  26. r.setSuccess(false);
  27. r.setCode(com.atguigu.common.utils.ResultCode.ERROR);
  28. r.setMessage("失败");
  29. return r;
  30. }
  31. public static R success(Boolean success){
  32. R r = new R();
  33. r.setSuccess(success);
  34. return r;
  35. }
  36. public R code(Integer code){
  37. this.setCode(code);
  38. return this;
  39. }
  40. public R message(String message){
  41. this.setMessage(message);
  42. return this;
  43. }
  44. public R data(String key, Object value){
  45. this.data.put(key, value);
  46. return this;
  47. }
  48. public R data(Map<String, Object> map){
  49. this.setData(map);
  50. return this;
  51. }
  52. }

七、修改讲师异常处理

  1. /**
  2. * 修改讲师
  3. * @param eduTeacher
  4. * @return
  5. */
  6. @PostMapping("updateTeacher")
  7. public R updateTeacher(@RequestBody EduTeacher eduTeacher){
  8. if(StringUtils.isEmpty(eduTeacher.getId())){
  9. throw new GuliException(2001,"讲师ID不能为空...");
  10. }
  11. LambdaQueryWrapper<EduTeacher> wrapper = new LambdaQueryWrapper<>();
  12. wrapper.eq(EduTeacher::getId, eduTeacher.getId());
  13. int count = eduTeacherService.count(wrapper);
  14. if(count == 0){
  15. throw new GuliException(2002, "讲师ID不存在....");
  16. }
  17. eduTeacherService.updateById(eduTeacher);
  18. return R.ok();
  19. }

八、修改讲师执行结果

图片.png