一文详解Spring中ResponseEntity包装器的使用

 更新时间:2025年02月11日 08:21:46   作者:唐青枫  
在 Spring 中,ResponseEntity 是 HTTP 响应的包装器,这篇文章主要为大家详细介绍了ResponseEntity包装器的使用,感兴趣的可以了解一下

简介

Spring 中,ResponseEntityHTTP 响应的包装器。它允许自定义响应的各个方面:

  • HTTP 状态码
  • 响应主体
  • HTTP 请求头

使用 ResponseEntity 允许完全控制 HTTP 响应,并且它通常用于 RESTful Web 服务中从控制器方法返回响应。

基本语法

ResponseEntity<T> response = new ResponseEntity<>(body, headers, status);
  • T:响应主体的类型
  • body:想要作为响应主体发送的对象(如果不想返回主体,则可以为空)
  • headers:想要包含的任何其他 HTTP 请求头
  • status:HTTP 状态代码(如 HttpStatus.OK、HttpStatus.CREATED等)

示例用法

基本用法:返回简单响应

@RestController
@RequestMapping("/api/posts")
public class PostController {

    @GetMapping("/{id}")
    public ResponseEntity<Post> getPost(@PathVariable Long id) {
        Post post = postService.findById(id);
        if (post != null) {
            return new ResponseEntity<>(post, HttpStatus.OK);  // 200 OK
        } else {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);  // 404 Not Found
        }
    }
}

返回带有请求头的 ResponseEntity

@GetMapping("/custom-header")
public ResponseEntity<String> getWithCustomHeader() {
    HttpHeaders headers = new HttpHeaders();
    headers.add("Custom-Header", "CustomValue");

    return new ResponseEntity<>("Hello with custom header!", headers, HttpStatus.OK);
}

返回具有创建状态的 ResponseEntity

创建新资源时,通常希望返回 201 Created 状态代码

@PostMapping("/create")
public ResponseEntity<Post> createPost(@RequestBody Post post) {
    Post createdPost = postService.save(post);
    URI location = ServletUriComponentsBuilder.fromCurrentRequest()
            .path("/{id}")
            .buildAndExpand(createdPost.getId())
            .toUri();

    return ResponseEntity.created(location).body(createdPost);
}

返回没有内容的 ResponseEntity

当成功处理一个请求但不需要返回任何内容(例如,一个 DELETE 请求)时,可以使用 204 No Content

@DeleteMapping("/{id}")
public ResponseEntity<Void> deletePost(@PathVariable Long id) {
    boolean isDeleted = postService.delete(id);
    if (isDeleted) {
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);  // 204 No Content
    } else {
        return new ResponseEntity<>(HttpStatus.NOT_FOUND);   // 404 Not Found
    }
}

使用带有异常处理的 ResponseEntity

可以在全局异常处理程序或控制器中使用 ResponseEntity 来处理异常

@ExceptionHandler(PostNotFoundException.class)
public ResponseEntity<String> handlePostNotFound(PostNotFoundException ex) {
    return new ResponseEntity<>(ex.getMessage(), HttpStatus.NOT_FOUND);
}

使用 Map 返回 ResponseEntity(例如,对于 JSON 响应)

@GetMapping("/user/{id}")
public ResponseEntity<Map<String, Object>> getUser(@PathVariable Long id) {
    Map<String, Object> response = new HashMap<>();
    User user = userService.findById(id);

    if (user != null) {
        response.put("status", "success");
        response.put("data", user);
        return new ResponseEntity<>(response, HttpStatus.OK);
    } else {
        response.put("status", "error");
        response.put("message", "User not found");
        return new ResponseEntity<>(response, HttpStatus.NOT_FOUND);
    }
}

具有泛型类型的 ResponseEntity

@GetMapping("/posts/{id}")
public ResponseEntity<Post> getPostById(@PathVariable Long id) {
    Post post = postService.findById(id);
    if (post != null) {
        return ResponseEntity.ok(post);  // 200 OK with Post object as body
    }
    return ResponseEntity.status(HttpStatus.NOT_FOUND).build();  // 404 Not Found with no body
}

// ResponseEntity.ok(post) 是 new ResponseEntity<>(post, HttpStatus.OK) 的简写

返回验证错误的 ResponseEntity

@PostMapping("/validate")
public ResponseEntity<Map<String, String>> validateUser(@RequestBody User user, BindingResult result) {
    if (result.hasErrors()) {
        Map<String, String> errorResponse = new HashMap<>();
        result.getFieldErrors().forEach(error -> errorResponse.put(error.getField(), error.getDefaultMessage()));
        return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST);  // 400 Bad Request
    }
    userService.save(user);
    return new ResponseEntity<>(HttpStatus.CREATED);  // 201 Created
}

使用统一的响应对象

1.定义统一响应对象

public class ApiResponse<T> {

    private String status;
    private String message;
    private T data;
    private ErrorDetails error;

    // Constructor for success response
    public ApiResponse(String status, String message, T data) {
        this.status = status;
        this.message = message;
        this.data = data;
    }

    // Constructor for error response
    public ApiResponse(String status, String message, ErrorDetails error) {
        this.status = status;
        this.message = message;
        this.error = error;
    }

    // Getters and setters
}

class ErrorDetails {
    private String timestamp;
    private int status;
    private String error;
    private String path;

    // Getters and setters
}

2.在控制器方法中使用统一响应

@GetMapping("/posts/{id}")
public ResponseEntity<ApiResponse<Post>> getPostById(@PathVariable Long id) {
    Post post = postService.findById(id);
    if (post != null) {
        ApiResponse<Post> response = new ApiResponse<>(
            "success", 
            "Post retrieved successfully", 
            post
        );
        return new ResponseEntity<>(response, HttpStatus.OK);
    } else {
        return getErrorResponse(HttpStatus.NOT_FOUND, "Post not found", "/api/posts/" + id);
    }
}
private ResponseEntity<ApiResponse<Post>> getErrorResponse(HttpStatus status, String message, String path) {
    ErrorDetails errorDetails = new ErrorDetails();
    errorDetails.setTimestamp(LocalDateTime.now().toString());
    errorDetails.setStatus(status.value());
    errorDetails.setError(status.getReasonPhrase());
    errorDetails.setPath(path);

    ApiResponse<Post> response = new ApiResponse<>(
        "error",
        message,
        errorDetails
    );

    return new ResponseEntity<>(response, status);
}

响应数据结构示例

1.Success

{
  "status": "success",
  "message": "Post retrieved successfully",
  "data": {
    "id": 1,
    "title": "Hello World",
    "content": "This is my first post"
  }
}

2.Error

{
  "status": "error",
  "message": "Post not found",
  "error": {
    "timestamp": "2025-02-07T06:43:41.111+00:00",
    "status": 404,
    "error": "Not Found",
    "path": "/api/posts/1"
  }
}

3.使用 @ControllerAdvice 全局统一处理异常

@ControllerAdvice
public class GlobalExceptionHandler {

    // Handle all exceptions
    @ExceptionHandler(Exception.class)
    public ResponseEntity<ApiResponse<Object>> handleGeneralException(Exception ex) {
        ErrorDetails errorDetails = new ErrorDetails();
        errorDetails.setTimestamp(LocalDateTime.now().toString());
        errorDetails.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
        errorDetails.setError("Internal Server Error");
        errorDetails.setPath("/api/posts");

        ApiResponse<Object> response = new ApiResponse<>("error", ex.getMessage(), errorDetails);
        return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR);
    }
}

常用的 HTTP 状态码

  • HttpStatus.OK:200 OK
  • HttpStatus.CREATED:201 Created
  • HttpStatus.NO_CONTENT:204 No Content
  • HttpStatus.BAD_REQUEST:400 Bad Request
  • HttpStatus.UNAUTHORIZED:401 Unauthorized
  • HttpStatus.FORBIDDEN:403 Forbidden
  • HttpStatus.NOT_FOUND:404 Not Found
  • HttpStatus.INTERNAL_SERVER_ERROR:500 Internal Server Error

到此这篇关于一文详解Spring中ResponseEntity包装器的使用的文章就介绍到这了,更多相关Spring ResponseEntity包装器内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • springboot项目中PropertySource如何读取yaml配置文件

    springboot项目中PropertySource如何读取yaml配置文件

    这篇文章主要介绍了springboot项目中PropertySource如何读取yaml配置文件问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-01-01
  • Spring动态多数据源配置实例Demo

    Spring动态多数据源配置实例Demo

    本篇文章主要介绍了Spring动态多数据源配置实例Demo,具有一定的参考价值,有兴趣的可以了解一下。
    2017-01-01
  • 在Trae IDE中创建Spring AI项目的实现步骤

    在Trae IDE中创建Spring AI项目的实现步骤

    Trae是一个下一代AI代码编辑器,具有智能代码生成、自然语言交互、图像辅助需求表达等功能,本文就来介绍一下Trae IDE创建Spring AI项目的实现步骤,感兴趣的可以了解一下
    2025-08-08
  • Intellij IDEA 2018配置Java运行环境的方法步骤

    Intellij IDEA 2018配置Java运行环境的方法步骤

    这篇文章主要介绍了Intellij IDEA 2018配置Java运行环境的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-09-09
  • Java实例化一个抽象类对象的方法教程

    Java实例化一个抽象类对象的方法教程

    大家都知道抽象类无法实例化,就无法创建对象。所以下面这篇文章主要给大家介绍了关于Java实例化一个抽象类对象的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧。
    2017-12-12
  • Spring Cache扩展功能实现过程解析

    Spring Cache扩展功能实现过程解析

    这篇文章主要介绍了Spring Cache扩展功能实现解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-02-02
  • JAVA中的字段校验(validation)

    JAVA中的字段校验(validation)

    这篇文章主要介绍了JAVA中的字段校验(validation)用法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-07-07
  • Spring解决循环依赖的方法及三级缓存机制实践案例

    Spring解决循环依赖的方法及三级缓存机制实践案例

    Spring通过三级缓存解决单例Bean循环依赖,但无法处理构造器、prototype作用域及@Async场景,建议使用setter注入、@Lazy注解和架构优化,遵循设计原则避免依赖问题,本文介绍Spring如何解决循环依赖:深入理解三级缓存机制,感兴趣的朋友一起看看吧
    2025-09-09
  • Java设计模式之模板模式(Template模式)介绍

    Java设计模式之模板模式(Template模式)介绍

    这篇文章主要介绍了Java设计模式之模板模式(Template模式)介绍,定义一个操作中算法的骨架,将一些步骤的执行延迟到其子类中,需要的朋友可以参考下
    2015-03-03
  • swagger注解@ApiModelProperty失效情况的解决

    swagger注解@ApiModelProperty失效情况的解决

    这篇文章主要介绍了swagger注解@ApiModelProperty失效情况的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-06-06

最新评论