资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

SpringBoot自定义返回错误码错误信息的方法

这篇文章主要介绍了Spring Boot自定义返回错误码错误信息的方法,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

创新互联建站"三网合一"的企业建站思路。企业可建设拥有电脑版、微信版、手机版的企业网站。实现跨屏营销,产品发布一步更新,电脑网络+移动网络一网打尽,满足企业的营销需求!创新互联建站具备承接各种类型的成都做网站、成都网站制作、成都外贸网站建设项目的能力。经过十余年的努力的开拓,为不同行业的企事业单位提供了优质的服务,并获得了客户的一致好评。

 说明

•在实际的开发过程中,很多时候要定义符合自己业务的错误码和错误信息,而不是统一的而不是统一的下面这种格式返回到调用端

INTERNAL_SERVER_ERROR(500, "Internal Server Error"),

下面我们来看看如何将我们自定义的错误码和错误信息返回到调用端。

1 自定义错误码

•首先我们要定义一个枚举类

public enum ErrorEnum {
  /*
   * 错误信息
   * */
  E_20011(20011, "缺少必填参数"),
  ;
  private Integer errorCode;
  private String errorMsg;
  ErrorEnum(Integer errorCode, String errorMsg) {
    this.errorCode = errorCode;
    this.errorMsg = errorMsg;
  }
  public Integer getErrorCode() {
    return errorCode;
  }
  public String getErrorMsg() {
    return errorMsg;
  }

2 定义一个异常类

•定义一个异常类继承RuntimeException类

public class BusinessException extends RuntimeException {
  private static final long serialVersionUID = 1L;
  private Integer code;
  /**
   * @param errorEnum 以错误的ErrorEnum做参数
   */
  public BusinessException(ErrorEnum errorEnum) {
    super(errorEnum.getErrorMsg());
    this.code = errorEnum.getErrorCode();
    this.resultJson = CommonUtil.errorJson(errorEnum);
  }
  public Integer getCode() {
    return code;
  }
  public void setCode(Integer code) {
    this.code = code;
  }
}

3 定义一个异常返回的模板类

•模板类定义了如何将异常通过什么形式进行返回。

public class ExceptionResponse {
  private String message;
  private Integer code;
  public ExceptionResponse(Integer code, String message) {
    this.message = message;
    this.code = code;
  }
  public static ExceptionResponse create(Integer code, String message) {
    return new ExceptionResponse(code, message);
  }
  public Integer getCode() {
    return code;
  }
  public String getMessage() {
    return message;
  }
}

4 定义全局处理 Controller 层异常

@ControllerAdvice
@Slf4j
public class ExceptionHandler {

  @ResponseBody
  @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
  @ExceptionHandler(Exception.class)
  public ExceptionResponse handleException(Exception ex) {
    if (ex instanceof BusinessException) {
      log.warn(ex.getMessage(), ex);
      BusinessException businessException = (BusinessException) ex;
      return ExceptionResponse.create(businessException.getCode(), businessException.getMessage());
    } else {
      log.error(ex.getMessage(), ex);
      return ExceptionResponse.create(HttpStatus.INTERNAL_SERVER_ERROR.value(), ex.getMessage());
    }
  }
}

5 演示效果

•定义Controller层

@PostMapping("test/exception")
  public String testException() {
    throw new BusinessException(ErrorEnum.E_20011);
  }

•通过postMan调用返回结果为

{ "message": "缺少必填参数", "code": 20011 }

感谢你能够认真阅读完这篇文章,希望小编分享的“Spring Boot自定义返回错误码错误信息的方法”这篇文章对大家有帮助,同时也希望大家多多支持创新互联,关注创新互联行业资讯频道,更多相关知识等着你来学习!


网站题目:SpringBoot自定义返回错误码错误信息的方法
文章起源:http://cdkjz.cn/article/pehjej.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220