当前位置: 首页 > news >正文

Spring MVC 处理 HTTP 状态码、响应头和异常的完整示例

Spring MVC 处理 HTTP 状态码、响应头和异常的完整示例


1. 正常响应处理

通过 ResponseEntity 可以灵活控制 HTTP 状态码、响应头和响应体。

代码示例:创建资源返回 201 并设置 Location 头
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

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

    @PostMapping
    public ResponseEntity<User> createUser(@RequestBody User user) {
        // 保存用户逻辑(假设已成功保存)
        user.setId(1L); // 假设生成的用户ID为1

        // 设置 Location 头指向新资源的 URI
        HttpHeaders headers = new HttpHeaders();
        headers.setLocation(ServletUriComponentsBuilder
                .fromCurrentRequest()
                .path("/{id}")
                .buildAndExpand(user.getId())
                .toUri());

        return new ResponseEntity<>(user, headers, HttpStatus.CREATED); // 201 Created
    }
}

代码示例:成功响应返回 200 并设置 Cache-Control 头
@GetMapping("/{id}")
public ResponseEntity<User> getUser(@PathVariable Long id) {
    User user = userService.findById(id); // 假设用户存在
    HttpHeaders headers = new HttpHeaders();
    headers.setCacheControl(CacheControl.noCache()); // 禁止缓存

    return new ResponseEntity<>(user, headers, HttpStatus.OK); // 200 OK
}

2. 异常处理

通过 @ResponseStatus@ControllerAdvice 实现异常状态码与响应控制。

自定义异常类(带 @ResponseStatus)
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

@ResponseStatus(code = HttpStatus.NOT_FOUND, reason = "User not found")
public class UserNotFoundException extends RuntimeException {
    public UserNotFoundException(String message) {
        super(message);
    }
}
全局异常处理类(@ControllerAdvice)
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(UserNotFoundException.class)
    public ResponseEntity<ErrorDetails> handleUserNotFound(
            UserNotFoundException ex, WebRequest request) {
        ErrorDetails error = new ErrorDetails(
                HttpStatus.NOT_FOUND.value(),
                ex.getMessage(),
                request.getDescription(false)
        );
        return new ResponseEntity<>(error, HttpStatus.NOT_FOUND);
    }

    @ExceptionHandler(Exception.class)
    public ResponseEntity<ErrorDetails> handleGeneralException(
            Exception ex, WebRequest request) {
        ErrorDetails error = new ErrorDetails(
                HttpStatus.INTERNAL_SERVER_ERROR.value(),
                "Internal Server Error",
                request.getDescription(false)
        );
        return new ResponseEntity<>(error, HttpStatus.INTERNAL_SERVER_ERROR);
    }

    // 辅助类:错误响应体
    private static class ErrorDetails {
        private int statusCode;
        private String message;
        private String path;

        public ErrorDetails(int statusCode, String message, String path) {
            this.statusCode = statusCode;
            this.message = message;
            this.path = path;
        }
        // 省略 getter/setter
    }
}

3. 响应头设置示例
@GetMapping("/custom-headers")
public ResponseEntity<String> customHeaders() {
    HttpHeaders headers = new HttpHeaders();
    headers.add("X-Custom-Header", "custom-value");
    headers.set("Content-Type", "text/plain");
    headers.set("Access-Control-Allow-Origin", "*");
    return new ResponseEntity<>("Response with custom headers", headers, HttpStatus.OK);
}

4. 关键注解与类说明
注解/类作用示例
@ResponseStatus在异常类上定义默认 HTTP 状态码和原因。@ResponseStatus(HttpStatus.NOT_FOUND)
ResponseEntity直接控制 HTTP 状态码、响应头和响应体。new ResponseEntity<>(data, headers, HttpStatus.OK)
@ControllerAdvice全局异常处理类,集中管理异常响应。@ControllerAdvice + @ExceptionHandler
HttpStatusHTTP 状态码枚举(如 HttpStatus.OK, HttpStatus.CREATED)。HttpStatus.NOT_FOUND

5. 场景总结表格
场景实现方式状态码响应头示例适用情况
成功创建资源ResponseEntity + HttpStatus.CREATED201Location: /api/users/1新资源创建成功后返回位置
返回成功数据ResponseEntity + HttpStatus.OK200Cache-Control: no-cache正常业务响应
资源不存在异常@ResponseStatus(HttpStatus.NOT_FOUND)404资源查询失败
全局异常处理(如服务器错误)@ControllerAdvice + @ExceptionHandler500捕获通用未处理异常
自定义响应头ResponseEntity 设置 HttpHeaders200X-Custom-Header: custom-value需要添加自定义响应头时

关键总结

  1. 状态码控制
    • ResponseEntity 直接指定状态码(如 HttpStatus.CREATED)。
    • @ResponseStatus 在异常类上定义默认状态码。
  2. 响应头管理
    • 通过 HttpHeaders 对象添加任意头信息。
  3. 异常处理
    • 自定义异常 + @ResponseStatus:针对特定异常返回状态码。
    • @ControllerAdvice:全局统一处理异常,返回结构化错误信息。
  4. 最佳实践
    • 使用 ResponseEntity 精确控制响应细节。
    • 通过 ErrorDetails 统一错误响应格式。
    • 对于常见 HTTP 状态码(如 404、500),优先使用标准枚举值。
http://www.dtcms.com/a/122397.html

相关文章:

  • 12.实现一个简单的依赖注入容器
  • [免费]SpringBoot+Vue高考志愿填报系统【论文+源码+SQL脚本】
  • MySQL | 三大日志文件
  • KHARPA币:结合传统与区块链技术的DeFi DAO革命
  • Houdini20.5apex绑定模块入门学习笔记
  • 参考平面跨分割情况下的信号回流
  • 落地DevOps文化:运维变革的正确打开方式
  • C#里设计Modbus-RTU(Remote Terminal Unit)协议
  • STM32——RTC实时时钟
  • Windows 部署项目 apache + mod_wsgi,nginx + waitress
  • 栈与堆的本质区别:深入理解 Rust 的内存管理模型
  • Xilinx虚拟输入/输出(VIO)IP核详细介绍及使用示例
  • Smith-Waterman 算法(C++实现)
  • SpringBoot 接口限流Lua脚本接合Redis 服务熔断 自定义注解 接口保护
  • postman 安装及使用 [软件测试工具]
  • 如何根据不同文字内容批量生产手写的图片,模拟真人写的笔记(待验证)
  • 代码随想录算法训练营Day24
  • 第1章 对大型语言模型的介绍
  • SQL优化技术分享:从 321 秒到 0.2 秒的性能飞跃 —— 基于 PawSQL 的 TPCH 查询优化实战
  • 栈与队列及其基础应用
  • 【Kafka基础】topic命令行工具kafka-topics.sh:基础操作命令解析
  • STM32低功耗
  • 数据结构--堆
  • 软件测试之功能测试详解
  • C++语法学习之路
  • Mac监控新风尚:酷炫界面,性能监控更直观!
  • 数字图像处理作业4
  • SQLite 中日期型数据定义及处理(Delphi 版本)
  • IDEA :物联网ThingsBoard-gateway配置,运行Python版本,连接thingsboard,接入 MQTT 设备
  • [ACM_1] 输入输出 | 多行 | 多组 | getline(cin,s) | cin处理