1.参数校验
    对前端传过来的参数进行校验,Java提供了heibernate校验接口,但是没有进行实现,前端只要穿过来一个参数有问题,应进行返回,这里需要配置validator校验bean
    /*
    参数校验,快速失败
    */
    @Bean
    public Validator validator(){
    ValidatorFactory factory= Validation.byProvider(HibernateValidator.class)
    .configure()
    .failFast(true)//hibernate参数校验,快速失败.只要有参数校验失败即返回
    .buildValidatorFactory();
    Validator validator = factory.getValidator();
    return validator;
    }
    然后在前端传过来的DTO对象上添加@NotNull注解或者@NotBlank注解,notblank注解是对传递过来的字符串进行非空判断
    2.异常处理
    配置全局异常处理类
    1.类上使用注解@RestControllerAdvice
    2.异常返回上添加异常声明注解,括号里面放置需要声明的异常类@ExceptionHandler(Exception.class),
    3.返回信息(返回状态码,返回信息,返回相对应的数据)
    状态码使用枚举类进行处理
    @Getter
    public enum ResultCode {
    // 枚举对象
    SUCCESS(200,”操作成功”),
    FAIL(500,”操作失败”),

    1. /* 数据错误:50001-599999 */<br /> _RESULT_DATA_NONE_(50001, "数据未找到"),<br /> _DATA_IS_WRONG_(50002, "数据有误"),<br /> _DATA_ALREADY_EXISTED_(50003, "数据已存在"),
    2. /* 参数错误:30001-39999 */<br /> _PARAM_IS_INVALID_(30001, "参数无效"),<br /> _PARAM_IS_BLANK_(30002, "参数为空"),<br /> _PARAM_TYPE_BIND_ERROR_(30003, "参数类型错误");
    3. // 字段<br /> private Integer code;<br /> private String message;
    4. // 私有构造器<br /> ResultCode(Integer code,String message){<br /> this.code = code;<br /> this.message = message;<br /> }<br />}<br />-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------<br />统一返回结果<br />@Data<br />@NoArgsConstructor<br />@AllArgsConstructor<br />public class Result<T> {<br /> @ApiModelProperty("响应状态码")<br /> private Integer code;
    5. @ApiModelProperty("响应信息")<br /> private String messsage;
    6. @ApiModelProperty("响应数据")<br /> private T obj;
    7. public static <T> Result<T> ok(){<br /> return new Result<>(ResultCode._SUCCESS_.getCode(),ResultCode._SUCCESS_.getMessage(),null);<br /> }
    8. public static <T> Result<T> ok(T obj){<br /> return new Result<>(ResultCode._SUCCESS_.getCode(),ResultCode._SUCCESS_.getMessage(),obj);<br /> }
    9. public static <T> Result<T> fail() {<br /> return new Result<>(ResultCode._FAIL_.getCode(),ResultCode._FAIL_.getMessage(),null);<br /> }
    10. public static <T> Result<T> fail(String message) {<br /> return new Result<>(ResultCode._FAIL_.getCode(),ResultCode._FAIL_.getMessage(),null);<br /> }
    11. public static <T> Result<T> fail(String message,T obj) {<br /> return new Result<>(ResultCode._FAIL_.getCode(),ResultCode._FAIL_.getMessage(),obj);<br /> }
    12. public static <T> Result<T> fail(ResultCode resultCode) {<br /> return new Result<>(resultCode.getCode(),resultCode.getMessage(),null);<br /> }<br />}<br />--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------<br />自定义分页对象<br />[@Data ](/Data ) <br />@ApiModel("封装分页结果")<br />[@Builder ](/Builder ) <br />[@NoArgsConstructor ](/NoArgsConstructor ) <br />[@AllArgsConstructor ](/AllArgsConstructor ) <br />public class PageBean {<br />[@ApiModelProperty(value ](/ApiModelProperty(value ) = "当前页") <br />private Integer pageNum;
    1. @ApiModelProperty(value = "页大小")
    2. private Integer pageSize;
    3. @ApiModelProperty(value = "总条数")
    4. private Long total;
    5. @ApiModelProperty(value = "总页数")
    6. private Integer pages;
    7. @ApiModelProperty(value = "当前页数据")
    8. private List<V> data;
    9. /**
    10. * 把PageInfo的属性赋值给自定义的PageBean对象
    11. * @param page
    12. * @param <V>
    13. * @return
    14. */
    15. public static <V>PageBean<V> convert(PageInfo<V> page){
    16. return PageBean.<V>builder()
    17. .pageNum( page.getPageNum())
    18. .pageSize(page.getSize())
    19. .total(page.getTotal())
    20. .data(page.getList())
    21. .pages(page.getPages())
    22. .build();
    23. }
    24. /**
    25. * 实现把MP提供的分页对象Page,转换为PageBean
    26. */
    27. public static <T> PageBean<T> fill(Page<T> page) {
    28. return PageBean.<T>builder()
    29. .pageNum((int) page.getCurrent())
    30. .pageSize((int) page.getSize())
    31. .total(page.getTotal())
    32. .pages((int) page.getPages())
    33. .data(page.getRecords())
    34. .build();
    35. }

    }