Spring 6/Spring Boot 3新特性:优雅的业务异常处理

开发 前端
当你使用Spring Boot(Spring MVC)进行RESTful API开发的时候,你会发现HTTP的状态码很多时候不能足够有效的传递错误的信息。

当你使用Spring Boot(Spring MVC)进行RESTful API开发的时候,你会发现HTTP的状态码很多时候不能足够有效的传递错误的信息。

HTTP里有一个RFC 7807规范:https://www.rfc-editor.org/rfc/rfc7807。这个规范里定义了HTTP API的“问题细节”(Problem Details)内容。

该规范定义了一个“问题细节”(Problem Details),用它来携带HTTP错误返回信息,避免自定义新的错误返回格式。我们通常情况下是自己定义错误返回格式的。

Spring 6.0为我们提供了一个org.springframework.http.ProblemDetail 来实现该规范。

RFC 7807是一个很简单的规范。它定义了一个JSON格式,并关联了一个媒体类型(media type),这个JSON格式包含了五个可选成员来描述问题细节:

type:一个URI引用,用来识别问题的类型。这个URI的路径内容应该用来显示人类可读的信息来描述类型;

title:人类可读的问题类型描述;相同类型的问题,应该总是相同的描述;

status:HTTP状态码,将它包含在问题细节里是一种方便的方式;

detail:人类可读的问题实例描述,解释为什么当前的问题发生在这个特定的场景下;

instance:一个URI引用,用来识别问题实例。这个URI的内容应该用来描述问题实例,但不是必须的。

我们首先建立一个演示项目:

1、通常的业务异常处理方法

定义一个业务异常类。异常含义为:当Person找不到的时候抛出的业务异常。

public class PersonNotFoundException  extends RuntimeException {
@Getter
private final HttpStatus status;

public PersonNotFoundException(String message, HttpStatus status){
super(message);
this.status = status;
}
}

注册这个业务异常到全局异常处理。

通过在@RestControllerAdvice 中定义全局的切面处理。

通过@ExceptionHandler 来处理指定异常的处理方式。

这里返回的格式就是我们自定义的ErrorMsg格式。我们通过自定义这个ErroMsg完成和接口使用者协议,完成对业务异常的处理。

@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(PersonNotFoundException.class)
public ResponseEntity<ErrorMsg> personNotFoundHandler(PersonNotFoundException e) {
ErrorMsg msg = new ErrorMsg("0000", e.getMessage());
return new ResponseEntity<>(msg, e.getStatus());
}
}

需自定义的错误返回

@Data
@AllArgsConstructor
@NoArgsConstructor
public class ErrorMsg {
private String code;
private String msg;
}

测试控制器

@RestController
public class PersonController {

@GetMapping("/getPerson")
public String getPerson(){
throw new PersonNotFoundException("查找的人不存在", HttpStatus.NOT_FOUND);
}
}

启动程序,访问:http://localhost:8080/getPerson

2、基于“问题细节”的业务异常处理

基于我们常规的异常处理,其实已经能满足我们的业务需求,使用RFC 7807规范,我们可以免去自定义的异常错误格式(ErrorMsg ),使用Spring 6.0给我们提供的ProblemDetail ,这样我们以后再无需自己自定义异常返回格式,且在不同的项目之间有了标准,从而客户端在使用的时候有了可预测性。

Spring 6.0的做法也很简单,我们只需要将我们自定返回的ErrorMsg 修改成ProblemDetail 即可。我们看一下示例代码:

@RestControllerAdvice
public class GlobalExceptionHandler {

@ExceptionHandler(PersonNotFoundException.class)
public ProblemDetail personNotFoundHandler(PersonNotFoundException e) {
ProblemDetail problemDetail = ProblemDetail.forStatus(HttpStatus.NOT_FOUND);
problemDetail.setType(URI.create("https://www.toutiao.com/c/user/token/MS4wLjABAAAAJxW0bvHKNNwIpcsocIDAjNHHNXg2yaj1upViHO2JVNw/"));
problemDetail.setTitle("Person Not Found");
problemDetail.setDetail(String.format("错误信息:‘%s’", e.getMessage()));
return problemDetail;
}
}

值得说的是ProblemDetail 还支持设置一个Map的properties:

private Map<String, Object> properties;

这样也为我们的定制扩展提供了更大的空间。

启动,访问:http://localhost:8080/getPerson,其实的错误返回符合RFC 7807规范。

注意查看返回的头信息,我们看到了,返回数据的媒体类型为:application/problem+json:

感谢支持我的书:《从企业级开发到云原生微服务:Spring Boot实战》

参考资料:https://docs.spring.io/spring-framework/docs/6.0.0-RC2/javadoc-api/org/springframework/http/ProblemDetail.html

文章出自:​​爱科学的卫斯理​​,如有转载本文请联系爱科学的卫斯理今日头条号。

责任编辑:武晓燕 来源: 爱科学的卫斯理
相关推荐

2021-04-20 10:50:38

Spring Boot代码Java

2022-04-08 16:27:48

SpringBoot异常处理

2021-09-03 06:46:34

Spring 6pring Boot 项目

2021-09-15 09:02:20

Spring 6Spring BootJava

2020-03-16 17:20:02

异常处理Spring Boot

2018-06-06 14:30:38

Spring BootApplication事件

2023-09-24 13:55:42

Spring应用程序

2009-06-18 15:40:07

Spring Batc

2009-06-24 09:22:04

Spring2.5新特

2018-05-30 15:10:24

Spring BootList类型

2021-04-30 07:34:01

Spring BootController项目

2009-06-15 16:15:37

Spring2.0新特

2022-11-08 07:46:28

record类声明代码

2023-04-17 23:49:09

开发代码Java

2018-08-20 16:25:48

编程语言Java异常处理

2021-03-09 13:18:53

加密解密参数

2023-07-10 08:00:13

架构Rest返回值

2011-05-24 09:22:44

Spring3异常处理

2022-06-04 12:25:10

解密加密过滤器

2023-09-13 08:56:51

点赞
收藏

51CTO技术栈公众号