统一异常处理
Posted aibaiyang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了统一异常处理相关的知识,希望对你有一定的参考价值。
1、GrilController
@PostMapping(value = "/grils") public Result<Gril> grilAdd(@Valid Gril gril,BindingResult bindingResult){ System.out.println("love:"+gril.getLove()); if (bindingResult.hasErrors()) { return ResultUtil.error(1, bindingResult.getFieldError().getDefaultMessage()); } /*直接得到传入参数*/ gril.setCupSize(gril.getCupSize()); gril.setAge(gril.getAge()); System.out.println("gril.getCupSize():"+gril.getCupSize()); System.out.println("gril.getAge():"+gril.getAge()); return ResultUtil.success(grilRepository.save(gril)); }
2、Service:
public void getAge(Integer id) throws Exception{ Gril gril = grilRepository.findOne(id); Integer age = gril.getAge(); if (age < 10) { //返回“你还在上小学吧” code=100 throw new GrilException(ResultEnum.PRIMARY_SCHOOL); }else if(age > 10 && age < 16){ //返回“你可能在上初中” code=101 throw new GrilException(ResultEnum.MIDDLE_SCHOOL); } //如果>16岁,加钱 //... }
3、GrilException
package com.imooc.exception; import com.imooc.enums.ResultEnum; public class GrilException extends RuntimeException{ private Integer code; public GrilException(ResultEnum resultEnum) { super(resultEnum.getMsg()); this.code = resultEnum.getCode(); } public Integer getCode() { return code; } public void setCode(Integer code) { this.code = code; } }
4、ResultEnum
public enum ResultEnum { UNKONW_ERROR(-1,"未知错误"), SUCCESS(0,"成功"), PRIMARY_SCHOOL(100,"你可能还在上小学"), MIDDLE_SCHOOL(101,"你可能再上初中"), ; private Integer code; private String msg; private ResultEnum(Integer code, String msg) { this.code = code; this.msg = msg; } public Integer getCode() { return code; } public String getMsg() { return msg; } }
5、ExceptionHandle(异常处理类)
package com.imooc.handle; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody; import com.imooc.constant.GrilConstant; import com.imooc.domain.Result; import com.imooc.exception.GrilException; import com.imooc.utils.ResultUtil; /** * @author by * */ @ControllerAdvice public class ExceptionHandle { @ExceptionHandler(value = Exception.class) @ResponseBody public Result handle(Exception e){ //instanceof用来判断内存中实际对象A是不是B类型 if (e instanceof GrilException) { //必须经过这一行代码,grilException对象才能调用GrilException里的方法 GrilException grilException = (GrilException) e; return ResultUtil.error(grilException.getCode(), grilException.getMessage()); }else{ return ResultUtil.error(GrilConstant.code,GrilConstant.grilException); } } }
6、ResultUtil
package com.imooc.utils; import java.util.List; import com.imooc.domain.Result; /** * @author by * */ public class ResultUtil { public static Result success(Object object){ Result result = new Result(); result.setCode(0); result.setMsg("成功"); result.setData(object); return result; } public static Result success(){ return success(null); } public static Result error(Integer code,String msg){ Result result = new Result(); result.setCode(code); result.setMsg(msg); result.setData(null); return result; } }
7、Result
package com.imooc.domain; /**http请求返回的最外层对象 * @author by * */ public class Result<T> { /*错误码*/ private Integer code; /*提示信息*/ private String msg; /*具体的内容*/ private T data; 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; } }
8、GrilConstant
package com.imooc.constant; public class GrilConstant { public static String grilException = "未知错误"; public static Integer code = -1; }
以上是关于统一异常处理的主要内容,如果未能解决你的问题,请参考以下文章