|
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
控制器
- @RestController
- public class TestController {
- @RequestMapping("/BusinessException")
- public String testResponseStatusExceptionResolver(@RequestParam("i") int i){
- if (i==0){
- throw new BusinessException(600,"自定义业务错误");
- }
- return "success";
- }
- }
复制代码实体类
- public class BusinessException extends RuntimeException{
- //自定义错误码
- private Integer code;
- //自定义构造器,必须输入错误码及内容
- public BusinessException(int code,String msg) {
- super(msg);
- this.code = code;
- }
- public Integer getCode() {
- return code;
- }
- public void setCode(Integer code) {
- this.code = code;
- }
- }
复制代码自定义业务处理业务异常类
- @ControllerAdvice
- public class CustomerBusinessExceptionHandler {
- @ResponseBody
- @ExceptionHandler(BusinessException.class)
- public Map<String, Object> businessExceptionHandler(BusinessException e) {
- Map<String, Object> map = new HashMap<String, Object>();
- map.put("code", e.getCode());
- map.put("message", e.getMessage());
- //发生异常进行日志记录,此处省略
- return map;
- }
- }
复制代码
|
|