admin管理员组

文章数量:1794759

JAVA后台捕获异常,返回异常信到前端

JAVA后台捕获异常,返回异常信到前端

1.编写自己捕获异常的包装类

标注绿色的是自己最近写的包装类

BusinessExceptionNew 类

package com.aostar.trademon.ExceptionHandler; import lombok.Data; /** * @author jay * @Description: 自定义业务异常类 * @date 2021/6/23 14:58 */ @Data public class BusinessExceptionNew extends RuntimeException { /** * 错误编码 */ private String code; public BusinessExceptionNew() { super(); } public BusinessExceptionNew(String message) { super(message); } public BusinessExceptionNew(String code, String message) { super(message); this.code = code; } public BusinessExceptionNew(Throwable cause) { super(cause); } public BusinessExceptionNew(String message, Throwable cause) { super(message, cause); } public BusinessExceptionNew(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { super(message, cause, enableSuppression, writableStackTrace); } public String getCode() { return code; } public void setCode(String code) { this.code = code; } @Override public String getMessage() { return super.getMessage(); } @Override public String toString() { return this.code + ":" + this.getMessage(); } }

SystemExceptionNew类

package com.aostar.trademon.ExceptionHandler; import lombok.Data; /** * @author jay * @Description: 自定义业务异常类 * @date 2021/6/23 14:58 */ @Data public class SystemExceptionNew extends RuntimeException { /** * 错误编码 */ private String code; public SystemExceptionNew() { super(); } public SystemExceptionNew(String message) { super(message); } public SystemExceptionNew(String code, String message) { super(message); this.code = code; } public SystemExceptionNew(Throwable cause) { super(cause); } public SystemExceptionNew(String message, Throwable cause) { super(message, cause); } public SystemExceptionNew(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { super(message, cause, enableSuppression, writableStackTrace); } public String getCode() { return code; } public void setCode(String code) { this.code = code; } @Override public String getMessage() { return super.getMessage(); } @Override public String toString() { return this.code + ":" + this.getMessage(); } }

ExceptionAdvice 类

package com.aostar.trademon.ExceptionHandler; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; /** * @author jay * @Description: * @date 2021/6/23 14:58 */ @RestController @ControllerAdvice public class ExceptionAdvice { public static Logger logger = LoggerFactory.getLogger(ExceptionAdvice.class); @ResponseBody @ExceptionHandler(SystemExceptionNew.class) public ResultVO handleException(Exception e) { ResultVO result = new ResultVO(); if (e instanceof BusinessException) { e = (BusinessException) e; result.setCode(((BusinessException) e).getCode()); } result.setMessage("系统异常信:"+e.getMessage()); return result; } @ExceptionHandler(RuntimeException.class) @ResponseBody public ResultVO handleException(RuntimeException e) { ResultVO result = new ResultVO(); result.setStatus("500"); result.setMessage("运行异常:"+e.getMessage()); return result; } @ExceptionHandler(BusinessExceptionNew.class) @ResponseBody public ResultVO doBusinessException(Exception e) { ResultVO result = new ResultVO(); result.setStatus("500"); result.setMessage("业务异常:"+e.getMessage()); return result; } }

返回前端的实体类

package com.aostar.trademon.ExceptionHandler; /** * @author jayd * @Description: * @date 2021/6/23 14:58 */ public class ResultVO { private String Code; private String Message; private String Status; public String getCode() { return Code; } public void setCode(String code) { Code = code; } public String getMessage() { return Message; } public void setMessage(String message) { Message = message; } public String getStatus() { return Status; } public void setStatus(String status) { Status = status; } } 2.使用方式

代码中throw new 的时候 如果有异常就会将异常信以ResultVO返回给前端 service层写

3.前台接收异常信 并进行展示

重点是几个注解的使用

1.Spring通过@ExceptionHandler来拦截系统运行时抛出的相应异常。其有效作用域是其所处的Controller,即它声明的异常处理方法无法拦截、处理其他Controller类中抛出的异常。

2.在类上使用@ControllerAdvice(控制器增强。该注解可以把其声明的类中使用@ExceptionHandler、@InitBinder、@ModelAttribute注解的方法应用到所有的 @RequestMapping注解的方法)声明一个拦截全局异常的@ExceptionHandler。

最后

深知大多数初中级JAVA工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《Java开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

小编已加密:aHR0cHM6Ly9kb2NzLnFxLmNvbS9kb2MvRFVrVm9aSGxQZUVsTlkwUnc==出于安全原因,我们把网站通过base64编码了,大家可以通过base64解码把网址获取下来。

本文标签: 异常信到后台java