package com.shop.common.exception; public enum BizCodeEnum { UNKNOW_EXCEPTION(10000,"系统未知异常"), VALID_EXCEPTION(10001,"参数校验异常"); private int code; private String msg; BizCodeEnum(int code,String msg){ this.code = code; this.msg = msg; } public int getCode() { return code; } public String getMsg() { return msg; } }
2.编写统一的异常处理类:
package com.shop.gulimall.product.exception; import com.shop.common.exception.BizCodeEnum; import com.shop.common.utils.R; import org.springframework.boot.context.properties.bind.BindResult; import org.springframework.validation.BindingResult; import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; import java.util.HashMap; import java.util.Map; // 集中处理所有异常 @RestControllerAdvice(basePackages = {"com.shop.gulimall.product.controller"}) public class GulimallExceptionControllerAdvice { @ExceptionHandler(value = MethodArgumentNotValidException.class) public R handleVaildException(MethodArgumentNotValidException e){ BindingResult bindResult = e.getBindingResult(); MaperrorMap = new HashMap (); bindResult.getFieldErrors().forEach(fieldError -> { errorMap.put(fieldError.getField(),fieldError.getDefaultMessage()); }); return R.error(BizCodeEnum.VALID_EXCEPTION.getCode(),BizCodeEnum.VALID_EXCEPTION.getMsg()).put("data",errorMap); } @ExceptionHandler(value = Throwable.class) public R handleException(Throwable throwable){ return R.error(BizCodeEnum.UNKNOW_EXCEPTION.getCode(),BizCodeEnum.UNKNOW_EXCEPTION.getMsg()); } }
3.实体类字段加上注解:
package com.shop.gulimall.product.entity; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import java.io.Serializable; import java.util.Date; import com.shop.common.valid.AddGroup; import com.shop.common.valid.UpdateGroup; import lombok.Data; import org.hibernate.validator.constraints.URL; import javax.validation.constraints.*; @Data @TableName("pms_brand") public class BrandEntity implements Serializable { private static final long serialVersionUID = 1L; @NotNull(message = "修改Id不能为空") @TableId private Long brandId; @NotBlank(message = "品牌名不能为空") private String name; @NotEmpty @URL(message = "不是合法地址") private String logo; private String descript; private Integer showStatus; @NotEmpty @Pattern(regexp = "/^[a-zA-Z]$/",message = "检索必须是一个字母") private String firstLetter; @NotNull @Min(value = 0,message = "排序最小为0") private Integer sort; }
4.使用:
在需要检验的参数前加上@Valid,如下:
@RequestMapping("/save") public R save(@Valid @RequestBody BrandEntity brand){ brandService.save(brand); return R.ok(); }
分组检验:
先定义两个空接口:
public interface UpdateGroup { }
public interface AddGroup { }
实体类字段加上注解,如果没有标注分组,则不生效:
@Data @TableName("pms_brand") public class BrandEntity implements Serializable { private static final long serialVersionUID = 1L; @Null(message = "新增不能指定Id",groups = {AddGroup.class}) @NotNull(message = "修改Id不能为空",groups = {UpdateGroup.class}) @TableId private Long brandId; @NotBlank(message = "品牌名不能为空",groups = {AddGroup.class,UpdateGroup.class}) private String name; @NotEmpty @URL(message = "不是合法地址") private String logo; private String descript; private Integer showStatus; @NotEmpty @Pattern(regexp = "/^[a-zA-Z]$/",message = "检索必须是一个字母") private String firstLetter; @NotNull @Min(value = 0,message = "排序最小为0") private Integer sort; }
controller加上注解@Validated ,如下:
@RequestMapping("/save") public R save(@Validated({AddGroup.class}) @RequestBody BrandEntity brand){ brandService.save(brand); return R.ok(); }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)