boot返回码规范 spring_SpringBoot中配置全局异常拦截器,并实现自定http错误码返回前端...

本文介绍了如何在SpringBoot中创建全局异常拦截器`GlobalExceptionAdvice`,利用`@ControllerAdvice`、`@ExceptionHandler`和`@ResponseStatus`处理异常,并返回统一格式的响应。此外,还讲解了通过创建自定义异常类如`HttpException`及其子类来设定不同的HTTP错误码,以更灵活地控制返回给前端的错误信息。在处理`HttpException`时,使用`ResponseEntity`构造带有自定义HTTP状态码的响应。
摘要由CSDN通过智能技术生成

创建拦截器类并标注@ControllerAdvice注解

@ControllerAdvice

public class GlobalExceptionAdvice {

//处理未知异常

@ExceptionHandler(Exception.class)

@ResponseBody

@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)

public UnifyResponse handleException(HttpServletRequest request, Exception e){

String method = request.getMethod();

String requestUrl = request.getRequestURI();

System.out.println(method);

System.out.println(requestUrl);

System.out.println(e);

UnifyResponse unifyResponse = new UnifyResponse(9999, "服务器异常", requestUrl + " " + method);

return unifyResponse;

}

}

public class UnifyResponse {

private Integer code;

private String message;

private String request;

public UnifyResponse(Integer code, String message, String request) {

this.code = code;

this.message = message;

this.request = request;

}

public UnifyResponse(){

}

@Override

public String toString() {

return "UnifyResponse{" +

"code=" + code +

", message='" + message + '\'' +

", request='" + request + '\'' +

'}';

}

/**

* 使用抛出异常的方式表示请求成功

*/

public static void crreateSuccess(){

throw new CreateSuccess(1);

}

}

在类中创建对异常的处理方法

@ExceptionHandler:指出该方法会处理哪些异常。

@ResponseStats:指定返回前端的错误码。

通过request与exception参数可获得请求与异常信息,并决定返回前端的错误信息。

以上方法基本可以满足大部分需求,但对于灵活调整返回前端的http错误码还是不太方便,需要对应每个http错误码建立不同的处理方法。

实现决定返回前端的消息的http错误码

public class HttpException extends RuntimeException{

protected Integer code;

protected Integer httpStatusCode = 500;

public Integer getCode() {

return code;

}

public Integer getHttpStatusCode() {

return httpStatusCode;

}

}

public class NotFoundException extends HttpException{

public NotFoundException(int code){

this.code = code;

this.httpStatusCode = 404;

}

}

我采用创建一个Http异常基类继承RuntimeException,然后根据不同的http错误码创建不同的异常类实现HttpException,并传入自己的错误码,不传入错误码则默认使用500

@ExceptionHandler(HttpException.class)

public ResponseEntity handleHttpException(HttpServletRequest request, HttpException e){

String method = request.getMethod();

String requestUrl = request.getRequestURI();

UnifyResponse unifyResponse = new UnifyResponse(e.getCode(), requestUrl + " " + method);

HttpHeaders headers = new HttpHeaders();

headers.setContentType(MediaType.APPLICATION_JSON);

HttpStatus httpStatus = HttpStatus.resolve(e.getHttpStatusCode());

ResponseEntity responseEntity = new ResponseEntity(unifyResponse, headers, httpStatus);

return responseEntity;

}

创建新的方法处理自定异常,为了改变返回消息的错误码需要设置http头部,并创建ResponseEntity将http头部与http错误码对象传入后返回。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值