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,”操作失败”),
/* 数据错误:50001-599999 */<br /> _RESULT_DATA_NONE_(50001, "数据未找到"),<br /> _DATA_IS_WRONG_(50002, "数据有误"),<br /> _DATA_ALREADY_EXISTED_(50003, "数据已存在"),
/* 参数错误:30001-39999 */<br /> _PARAM_IS_INVALID_(30001, "参数无效"),<br /> _PARAM_IS_BLANK_(30002, "参数为空"),<br /> _PARAM_TYPE_BIND_ERROR_(30003, "参数类型错误");
// 字段<br /> private Integer code;<br /> private String message;
// 私有构造器<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;
@ApiModelProperty("响应信息")<br /> private String messsage;
@ApiModelProperty("响应数据")<br /> private T obj;
public static <T> Result<T> ok(){<br /> return new Result<>(ResultCode._SUCCESS_.getCode(),ResultCode._SUCCESS_.getMessage(),null);<br /> }
public static <T> Result<T> ok(T obj){<br /> return new Result<>(ResultCode._SUCCESS_.getCode(),ResultCode._SUCCESS_.getMessage(),obj);<br /> }
public static <T> Result<T> fail() {<br /> return new Result<>(ResultCode._FAIL_.getCode(),ResultCode._FAIL_.getMessage(),null);<br /> }
public static <T> Result<T> fail(String message) {<br /> return new Result<>(ResultCode._FAIL_.getCode(),ResultCode._FAIL_.getMessage(),null);<br /> }
public static <T> Result<T> fail(String message,T obj) {<br /> return new Result<>(ResultCode._FAIL_.getCode(),ResultCode._FAIL_.getMessage(),obj);<br /> }
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;
@ApiModelProperty(value = "页大小")
private Integer pageSize;
@ApiModelProperty(value = "总条数")
private Long total;
@ApiModelProperty(value = "总页数")
private Integer pages;
@ApiModelProperty(value = "当前页数据")
private List<V> data;
/**
* 把PageInfo的属性赋值给自定义的PageBean对象
* @param page
* @param <V>
* @return
*/
public static <V>PageBean<V> convert(PageInfo<V> page){
return PageBean.<V>builder()
.pageNum( page.getPageNum())
.pageSize(page.getSize())
.total(page.getTotal())
.data(page.getList())
.pages(page.getPages())
.build();
}
/**
* 实现把MP提供的分页对象Page,转换为PageBean
*/
public static <T> PageBean<T> fill(Page<T> page) {
return PageBean.<T>builder()
.pageNum((int) page.getCurrent())
.pageSize((int) page.getSize())
.total(page.getTotal())
.pages((int) page.getPages())
.data(page.getRecords())
.build();
}
}