目前我这里处理的一种是404、500等状态码,另外一个就是自定义code码在程序中try()catch(){}的时候throw()
这两个基本可以覆盖全了
一、先说状态码吧
/**
* @author: tianyong
* @Time: 2019/7/3 11:00
* @description:错误码包装枚举类
*/
public enum CodeInfo {
//状态码
STATUS_CODE_400(400,"请求无效"),
STATUS_CODE_403(403,"禁止访问"),
STATUS_CODE_404(404,"请求的网页不存在"),
STATUS_CODE_405(405,"资源被禁止"),
STATUS_CODE_500(500,"内部服务器错误,请联系管理员"),
//自定义状态码
CHART_INVALID_PARAM(100002,"图表请求参数错误"),
PAGE_INVALID_PARAM(100003,"页面请求参数错误"),
MENU_INVALID_PARAM(100004,"菜单请求参数错误"),
CHART_FIND_SUCCESS(100005,"查询结果为空");
......
private String msg;
private Integer code;
CodeInfo(Integer code, String msg) {
this.msg = msg;
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
@Override
public String toString() {
return "CodeInfo{" +
"msg='" + msg + '\'' +
", code=" + code +
'}';
}
}
/**
* @author: tianyong
* @Time: 2019/7/15 15:54
* @description:状态码配置类
*/
@Controller
public class StatusCodeConfig implements ErrorController {
/**
* @author: tianyong
* @time: 2019/7/16 11:36
* @description:指定跳转路径/error
*/
@Override
public String getErrorPath() {
return "/error";
}
/**
* @author: tianyong
* @time: 2019/7/16 11:37
* @description:处理错误状态码
*/
@RequestMapping("/error")
public String handleError(HttpServletRequest request) {
Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
if(statusCode == 400) {
return "/400";
}else if(statusCode == 403) {
return "/403";
}else if(statusCode == 404) {
return "/404";
}else if(statusCode == 405) {
return "/405";
}else if(statusCode == 500) {
return "/500";
}else{
return "/500";
}
}
/**
* @author: tianyong
* @time: 2019/7/15 16:14
* @description:400 请求无效
*/
@ResponseBody
@RequestMapping("/400")
public ReturnBone error400 (){
return error(CodeInfo.STATUS_CODE_400);
}
/**
* @author: tianyong
* @time: 2019/7/15 16:14
* @description:403 禁止访问
*/
@ResponseBody
@RequestMapping("/403")
public ReturnBone error403 (){
return error(CodeInfo.STATUS_CODE_403);
}
/**
* @author: tianyong
* @time: 2019/7/15 16:14
* @description:404 请求的网页不存在
*/
@ResponseBody
@RequestMapping("/404")
public ReturnBone error404 (){
return error(CodeInfo.STATUS_CODE_404);
}
/**
* @author: tianyong
* @time: 2019/7/15 16:14
* @description:405 资源被禁止
*/
@ResponseBody
@RequestMapping("/405")
public ReturnBone error405 (){
return error(CodeInfo.STATUS_CODE_405);
}
/**
* @author: tianyong
* @time: 2019/7/15 16:14
* @description:500 内部服务器错误,请联系管理员
*/
@ResponseBody
@RequestMapping("/500")
public ReturnBone error500 (){
return error(CodeInfo.STATUS_CODE_500);
}
}
状态码的使用,当程序遇到空指针,此时没有用自定义code码包裹,就会用到返回500的状态码
二、自定义code码
/**
* @author: tianyong
* @Time: 2019/6/4 11:27
* @description:resultful接口返回结构
*/
public class ReturnBone {
/*返回码*/
private Integer code;
/*提示信息 */
private String msg;
/*数据*/
private T data;
public ReturnBone() {}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
@Override
public String toString() {
return "ReturnBone{" +
"code=" + code +
", msg='" + msg + '\'' +
", data=" + data +
'}';
}
}
/**
* @author: tianyong
* @Time: 2019/7/16 11:01
* @description:状态码对象(错误)
*/
public class StatusCodeBO extends RuntimeException{
private CodeInfo codeInfo;
public StatusCodeBO(CodeInfo codeInfo){
this.codeInfo = codeInfo;
makeInfo();
}
public ReturnBone makeInfo() {
ReturnBone rb = new ReturnBone();
rb.setCode(codeInfo.getCode());
rb.setMsg(codeInfo.getMsg());
return rb;
}
}
/**
* @author: tianyong
* @Time: 2019/7/15 16:08
* @description:自定义异常处理类
*/
@ControllerAdvice
public class CustomExceptionHandler {
/**
* @author: tianyong
* @time: 2019/7/15 16:10
* @description:自定义异常处理方法
*/
@ResponseBody
@ExceptionHandler({StatusCodeBO.class})
public ReturnBone HandlerException(StatusCodeBO info){
return info.makeInfo();
}
}
使用 if(datas.size() == 0) throw new StatusCodeBO(CodeInfo.CHART_FIND_SUCCESS);
好好理解理解,胸弟,我这边针对网上的,和我这里现实的统一封装结果的,做了一些整合,肯定好使的