- 作 者:是江迪呀
- ✒️本文关键词:
SpringBoot
、模版
、企业级
- ☀️每日 一言:
你坚持下来了,而别人坚持不下来,这就是你的资本。
本次需要的依赖:
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>1.16.16version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>fastjsonartifactId>
<version>1.2.75version>
dependency>
最终项目结构:
在一个项目中我们常常需要处理一些业务异常,自定义一些状态码,SpringBoot框架中也有对应的方法供我们使用,但是为方便后续的扩展我们还是要自定义一个全局异常。
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
@ExceptionHandler(value = BusinessException.class)
@ResponseBody
public Response businessExceptionHandler(HttpServletRequest request, BusinessException e){
log.error("发生业务异常!原因是:{}",e.getErrorMsg());
return Response.error(e.getErrorCode(),e.getErrorMsg());
}
/**
* 处理空指针的异常
* @param req
* @param e
* @return
*/
@ExceptionHandler(value =NullPointerException.class)
@ResponseBody
public Response exceptionHandler(HttpServletRequest req, NullPointerException e){
log.error("发生空指针异常!原因是:",e);
return Response.error(ResultEnum.BIZ_EXCEPTION.getValue());
}
/**
* 处理其他异常
* @param req
* @param e
* @return
*/
@ExceptionHandler(value =Exception.class)
@ResponseBody
public Response exceptionHandler(HttpServletRequest req, Exception e){
log.error("系统异常!原因是:",e);
return Response.error(ResultEnum.BIZ_EXCEPTION.getValue());
}
}
@ExceptionHandler(value = BusinessException.class)
代码中的 value值就是 处理业务异常类。public class BusinessException extends RuntimeException {
private static final long serialVersionUID = 1L;
/**
* 错误码
*/
protected String errorCode;
/**
* 错误信息
*/
protected String errorMsg;
public BusinessException() {
super();
}
public BusinessException(String errorMsg) {
super(errorMsg);
this.errorMsg = errorMsg;
this.errorCode = ResultEnum.FAILED.getCode();
}
public BusinessException(String errorCode, String errorMsg) {
super(errorCode);
this.errorCode = errorCode;
this.errorMsg = errorMsg;
}
public BusinessException(String errorCode, String errorMsg, Throwable cause) {
super(errorCode, cause);
this.errorCode = errorCode;
this.errorMsg = errorMsg;
}
public String getErrorCode() {
return errorCode;
}
public void setErrorCode(String errorCode) {
this.errorCode = errorCode;
}
public String getErrorMsg() {
return errorMsg;
}
public void setErrorMsg(String errorMsg) {
this.errorMsg = errorMsg;
}
public String getMessage() {
return errorMsg;
}
@Override
public Throwable fillInStackTrace() {
return this;
}
}
public enum ResultEnum {
SUCCESS("0","执行成功"),
FAILED("-1","执行失败"),
BIZ_EXCEPTION("500","系统异常"),
IDENTITY_IS_OVERDUE("1","身份过期"),
//可以继续扩展定义
;
private String code;
private String value;
ResultEnum(String code, String value) {
this.code = code;
this.value = value;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
注意:需要继承RuntimeException
异常,因为我们处理的业务异常全部都是运行时异常。
在需要抛出业务异常的地方:
//自定义提示信息
throw new BusinessException("发生了业务异常!");
//自定义状态码
throw new BusinessException("502","发生了业务异常!");
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements UserService {
@Override
public List<UserVO> queryUserInfo() {
int a = 1;
if(a == 1){
throw new BusinessException("502","发生了业务异常!");
}
return baseMapper.queryUserInfo();
}
}
在上篇文章中我们在controller层返回的值直接是个集合:
这样是不符合规范的,我们需要定义一个含有code状态码、msg提示信息、data数据这三者的返回体。
@Getter
@Setter
public class Response {
/**
* 响应码
*/
private String code;
/**
* 响应消息
*/
private String msg;
/**
* 响应结果
*/
private Object result;
/**
* 成功
*
* @return
*/
public static Response success() {
return new Response(ResultEnum.SUCCESS.getCode(),ResultEnum.SUCCESS.getValue(),null);
}
/**
* 成功
*
* @return
*/
public static Response success(String msg) {
return new Response(ResultEnum.SUCCESS.getCode(),msg,null);
}
public Response() {
}
public Response(String code, String msg, Object result) {
this.code = code;
this.msg = msg;
this.result = result;
}
/**
* 成功
* @param data
* @return
*/
public static Response success(Object data) {
Response rb = new Response();
rb.setCode(ResultEnum.SUCCESS.getCode());
rb.setMsg(ResultEnum.SUCCESS.getValue());
rb.setResult(data);
return rb;
}
/**
* 失败
*/
public static Response error(String code, String message) {
Response rb = new Response();
rb.setCode(code);
rb.setMsg(message);
rb.setResult(null);
return rb;
}
/**
* 失败
*/
public static Response error(String message) {
Response rb = new Response();
rb.setCode(ResultEnum.FAILED.getCode());
rb.setMsg(message);
rb.setResult(null);
return rb;
}
@Override
public String toString() {
return JSONObject.toJSONString(this);
}
}
public abstract class AbstractController {
public Response returnSuccess(){
return Response.success();
}
public Response returnSuccess(Object data){
return Response.success(data);
}
public Response returnSuccess(String msg){
return Response.success(msg);
}
}
我们需要每个controller都继承AbstractController
@RestController
@RequestMapping("/user")
public class UserController extends AbstractController {
@Autowired
private UserService userService;
@GetMapping("/users")
public Response users(){
return returnSuccess(userService.queryUserInfo());
}
}