一、请求参数加校验
package com.atguigu.eduservice.entity.reqVo;
import com.atguigu.common.valid.ListValue;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import java.io.Serializable;
@Data
public class TeacherVo implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "讲师姓名")
@NotBlank(message = "讲师姓名必须提交")
@Size(min = 1, max = 6)
private String name;
@ApiModelProperty(value = "讲师简介")
@NotBlank(message = "讲师简介必须提交")
private String intro;
@ApiModelProperty(value = "讲师资历,一句话说明讲师")
private String career;
@ApiModelProperty(value = "头衔 1高级讲师 2首席讲师")
@ListValue(vals={1,2},message = "只能输入1或者2")
private Integer level;
@ApiModelProperty(value = "讲师头像")
private String avatar;
@ApiModelProperty(value = "排序")
@Min(value = 0,message = "排序必须大于等于0")
private Integer sort;
}
二、controller的方法上加注解:@Validated
/**
* 新增讲师
* @param teacherVo
* @return
*/
@PostMapping("addTeacher")
public R addTeacher(@Validated @RequestBody TeacherVo teacherVo){
EduTeacher eduTeacher = new EduTeacher();
BeanUtils.copyProperties(teacherVo, eduTeacher);
eduTeacher.setGmtCreate(new Date());
eduTeacher.setGmtModified(new Date());
eduTeacherService.save(eduTeacher);
return R.ok();
}
三、集中处理所有controller抛过来的异常
package com.atguigu.eduservice.exception;
/**
* 集中处理所有controller抛过来的异常
*/
@Slf4j
//@ResponseBody
//@ControllerAdvice(basePackages = "com.atguigu.eduservice.controller")
@RestControllerAdvice(basePackages = "com.atguigu.eduservice.controller")
public class ExceptionControllerAdvice {
/**
* 处理controller数据校验错误。精确处理异常
* @param e
* @return
*/
@ExceptionHandler(value= MethodArgumentNotValidException.class)
public R handleVaildException(MethodArgumentNotValidException e){
log.error("数据校验出现问题{},异常类型:{}",e.getMessage(),e.getClass());
BindingResult bindingResult = e.getBindingResult();
Map<String,Object> errorMap = new HashMap<>();
bindingResult.getFieldErrors().forEach((fieldError)->{
// 获取到错误提示
Object message = fieldError.getDefaultMessage();
// 获取错误属性的名字
String field = fieldError.getField();
errorMap.put(field,message);
});
return R.error().data(errorMap);
}
/**
* 处理自定义的异常
* @param throwable
* @return
*/
@ExceptionHandler(value = GuliException.class)
public R handleGuliException(GuliException e){
log.error("错误:",e);
return R.success(false).code(e.getCode()).message(e.getMsg());
}
/**
* 处理所有类型的异常
* @param throwable
* @return
*/
@ExceptionHandler(value = Throwable.class)
public R handleException(Throwable throwable){
log.error("错误:",throwable);
return R.error();
}
}
四、新增讲师执行结果
扩展:自定义校验注解
@ApiModelProperty(value = "头衔 1高级讲师 2首席讲师")
@ListValue(vals={1,2},message = "只能输入1或者2")
private Integer level;
package com.atguigu.common.valid;
//ListValueConstraintValidator 自定义的校验器
@Documented
@Constraint(validatedBy = { ListValueConstraintValidator.class })
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE })
@Retention(RUNTIME)
public @interface ListValue {
// 在配置文件ValidationMessages.properties中
String message() default "{com.atguigu.common.valid.ListValue.message}";
Class<?>[] groups() default { };
Class<? extends Payload>[] payload() default { };
int[] vals() default { };
}
自定义校验器
package com.atguigu.common.valid;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;
import java.util.Set;
public class ListValueConstraintValidator implements ConstraintValidator<ListValue,Integer> {
private Set<Integer> set = new HashSet<>();
//初始化方法
@Override
public void initialize(ListValue constraintAnnotation) {
// 获取属性上得所有数据
int[] vals = constraintAnnotation.vals();
for (int val : vals) {
set.add(val);
}
}
/**
* 判断是否校验成功
* @param value 需要校验的值
* @param context 整个上下文环境信息
* @return 判断需要校验的值是否在属性上的所有枚举值
*/
@Override
public boolean isValid(Integer value, ConstraintValidatorContext context) {
return set.contains(value);
}
}
五、自定义异常
package com.atguigu.common.exceptionhandler;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor //生成有参数构造方法
@NoArgsConstructor //生成无参数构造
public class GuliException extends RuntimeException {
private Integer code; //状态码
private String msg; //异常信息
}
六、统一返回结果类
package com.atguigu.common.utils;
//统一返回结果的类
@Data
public class R {
@ApiModelProperty(value = "是否成功")
private Boolean success;
@ApiModelProperty(value = "返回码")
private Integer code;
@ApiModelProperty(value = "返回消息")
private String message;
@ApiModelProperty(value = "返回数据")
private Map<String, Object> data = new HashMap<String, Object>();
//把构造方法私有
private R() {}
//成功静态方法
public static R ok() {
R r = new R();
r.setSuccess(true);
r.setCode(com.atguigu.common.utils.ResultCode.SUCCESS);
r.setMessage("成功");
return r;
}
//失败静态方法
public static R error() {
R r = new R();
r.setSuccess(false);
r.setCode(com.atguigu.common.utils.ResultCode.ERROR);
r.setMessage("失败");
return r;
}
public static R success(Boolean success){
R r = new R();
r.setSuccess(success);
return r;
}
public R code(Integer code){
this.setCode(code);
return this;
}
public R message(String message){
this.setMessage(message);
return this;
}
public R data(String key, Object value){
this.data.put(key, value);
return this;
}
public R data(Map<String, Object> map){
this.setData(map);
return this;
}
}
七、修改讲师异常处理
/**
* 修改讲师
* @param eduTeacher
* @return
*/
@PostMapping("updateTeacher")
public R updateTeacher(@RequestBody EduTeacher eduTeacher){
if(StringUtils.isEmpty(eduTeacher.getId())){
throw new GuliException(2001,"讲师ID不能为空...");
}
LambdaQueryWrapper<EduTeacher> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(EduTeacher::getId, eduTeacher.getId());
int count = eduTeacherService.count(wrapper);
if(count == 0){
throw new GuliException(2002, "讲师ID不存在....");
}
eduTeacherService.updateById(eduTeacher);
return R.ok();
}
八、修改讲师执行结果