Spring Boot 3 构建统一的请求响应参数、异常处理、以及统一的异常状态码

在 Spring Boot 3 微服务中,构建统一的请求响应参数、异常处理、以及统一的异常状态码是标准化和提高可维护性的关键。以下是实现的最佳实践:

1. 统一请求响应结构

定义统一的响应结构

创建一个通用的响应包装类 ApiResponse:

java 复制代码
public class ApiResponse<T> {
    private int code;         // 状态码
    private String message;   // 消息
    private T data;           // 数据

    public ApiResponse(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static <T> ApiResponse<T> success(T data) {
        return new ApiResponse<>(200, "Success", data);
    }

    public static <T> ApiResponse<T> success(String message, T data) {
        return new ApiResponse<>(200, message, data);
    }

    public static <T> ApiResponse<T> error(int code, String message) {
        return new ApiResponse<>(code, message, null);
    }

    // Getters and Setters
}

统一返回格式的切面处理

使用 Spring 的 @ControllerAdvice 对所有 @RestController 返回的内容进行统一封装:

java 复制代码
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

@RestControllerAdvice
public class GlobalResponseHandler extends ResponseEntityExceptionHandler {

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ApiResponse<Object> handleException(Exception ex) {
        return ApiResponse.error(500, "Internal Server Error: " + ex.getMessage());
    }

    @ExceptionHandler(CustomException.class)
    @ResponseBody
    public ApiResponse<Object> handleCustomException(CustomException ex) {
        return ApiResponse.error(ex.getCode(), ex.getMessage());
    }
}

2. 统一异常处理

自定义异常类

创建一个通用的业务异常类 CustomException:

java 复制代码
public class CustomException extends RuntimeException {
    private int code;

    public CustomException(int code, String message) {
        super(message);
        this.code = code;
    }

    public int getCode() {
        return code;
    }
}

全局异常处理

在 @RestControllerAdvice 中捕获自定义异常和其他常见异常:

java 复制代码
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(CustomException.class)
    public ApiResponse<Object> handleCustomException(CustomException ex) {
        return ApiResponse.error(ex.getCode(), ex.getMessage());
    }

    @ExceptionHandler(IllegalArgumentException.class)
    public ApiResponse<Object> handleIllegalArgumentException(IllegalArgumentException ex) {
        return ApiResponse.error(400, "Invalid Argument: " + ex.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public ApiResponse<Object> handleGeneralException(Exception ex) {
        return ApiResponse.error(500, "Internal Server Error: " + ex.getMessage());
    }
}

3. 统一状态码管理

定义状态码枚举

创建一个枚举类,管理所有可能的状态码:

java 复制代码
public enum ApiStatus {
    SUCCESS(200, "Success"),
    BAD_REQUEST(400, "Bad Request"),
    UNAUTHORIZED(401, "Unauthorized"),
    FORBIDDEN(403, "Forbidden"),
    NOT_FOUND(404, "Not Found"),
    INTERNAL_SERVER_ERROR(500, "Internal Server Error"),
    CUSTOM_ERROR(1001, "Custom Business Error");

    private final int code;
    private final String message;

    ApiStatus(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public int getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }
}

在 CustomException 中使用状态码

java 复制代码
public class CustomException extends RuntimeException {
    private int code;

    public CustomException(ApiStatus status) {
        super(status.getMessage());
        this.code = status.getCode();
    }

    public int getCode() {
        return code;
    }
}

4. Controller 示例

在 Controller 中使用统一的返回结构和异常处理:

java 复制代码
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api")
public class UserController {

    @GetMapping("/user/{id}")
    public ApiResponse<User> getUser(@PathVariable Long id) {
        if (id <= 0) {
            throw new CustomException(ApiStatus.BAD_REQUEST);
        }

        // 模拟用户数据
        User user = new User(id, "John Doe", "john.doe@example.com");
        return ApiResponse.success(user);
    }
}

6. 总结

核心实现步骤:

统一响应格式:

定义 ApiResponse。

使用 @RestControllerAdvice 封装统一响应。

统一异常处理:

创建自定义异常类 CustomException。

使用全局异常处理器 @RestControllerAdvice 捕获异常。

统一状态码管理:

定义枚举类 ApiStatus。

使用状态码驱动异常和响应。

相关推荐
m0_748255262 分钟前
Spring Boot 3.x 引入springdoc-openapi (内置Swagger UI、webmvc-api)
spring boot·后端·ui
小华同学ai6 分钟前
吊打中文合成!这款开源语音神器效果炸裂,逼真到离谱!
前端·后端·github
语落心生13 分钟前
算法计算与训练如何支持低开销流式计算? deepseek背后的smallpond需要些新改造
后端
DavidSoCool15 分钟前
Elasticsearch Java API Client [8.17] 使用
java·大数据·elasticsearch
无世世16 分钟前
【Java从入门到起飞】面向对象编程(高级)
java·开发语言
Vic1010133 分钟前
Mac如何查看 IDEA 的日志文件
java·macos·intellij-idea
陈逸轩*^_^*34 分钟前
idea打不开,idea打不开,Error occurred during initialization of VM
java·ide·intellij-idea
=PNZ=BeijingL37 分钟前
使用Mockito实现单元测试
java
uhakadotcom41 分钟前
Python高并发实战:阿里云函数计算 + 异步编程高效处理万人请求
后端·面试·github