[payload-spring-boot-starter] SpringBoot 统一响应体/异常处理组件

payload-spring-boot-starter

一个 SpringBoot 统一响应体/异常处理组件

信息

基于 spring-boot-starter-web:2.6.3 构建

by:林同学([email protected]

Getting Started

引入依赖:


    com.github.LinYuanBaoBao
    payload-spring-boot-starter
    1.1.0-RELEASE

Controller 上使用 @Payload 注解,自动对返回的对象进行包装:

@Payload
@RequestMapping("/users")
@RestController
public class MyController {

   @GetMapping("/{id}")
   public User get(@PathVariable Integer id) {
       return user;
   }

}

响应结果如下:

{
  "code": 200,
  "data": {
    "k1": "v1",
    "k2": "v2"
  },
  "success": true,
  "timestamp": 1623055152059
}

业务异常

开启业务异常处理:

spring:
  mvc:
    payload:
      biz-error-enabled: true

抛出业务异常 BizException

throw new BizException("业务异常",9999);    

此时 http-status 状态码为 400,响应结果如下:

{
    "success": false,
    "message": "业务异常",
    "code": 9999,
    "timestamp": 1623055152059
}

或者,你也可以继承 GlobalExceptionHandler 类自定义异常处理逻辑

你可能感兴趣的:([payload-spring-boot-starter] SpringBoot 统一响应体/异常处理组件)