Spring/Boot接口定义,统一处理 (异常/识别码)
作者:互联网
结果集实体类
package entity;
public class Result {
private boolean flag;//是否成功
private Integer code;// 返回码
private String message;//返回信息
private Object data;// 返回数据
//省略Get / Set
}
分页结果集实体类
package entity;
import java.util.List;
public class PageResult<T> {
private Long total;
private List<T> rows;
//省略Get / Set
}
状态码实体类
package entity;
public class StatusCode {
public static final int OK=20000;//成功
public static final int ERROR =20001;//失败
public static final int LOGINERROR =20002;//用户名或密码错误
public static final int ACCESSERROR =20003;//权限不足
public static final int REMOTEERROR =20004;//远程调用失败
public static final int REPERROR =20005;//重复操作
}
接口返回异常处理
@RestControllerAdvice
public class BaseExceptionHandler {
@ExceptionHandler(value = Exception.class)
public Result exception(Exception e){
e.printStackTrace();
return new Result(false, StatusCode.ERROR,e.getMessage(),"");
}
}
测试Controller
@RestController
@CrossOrigin
@RequestMapping("/label")
public class LabelController {
@Autowired
private LabelService labelService;
@RequestMapping(method = RequestMethod.GET)
public Result findAll(){
return new Result(true, StatusCode.OK, "查询成功",labelService.findAll());
}
}
标签:int,Spring,class,Boot,private,final,static,接口定义,public 来源: https://www.cnblogs.com/YangGC/p/12220978.html