一文详解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包装器内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • idea快捷键生成getter和setter,有构造参数,无构造参数,重写toString方式

    idea快捷键生成getter和setter,有构造参数,无构造参数,重写toString方式

    这篇文章主要介绍了java之idea快捷键生成getter和setter,有构造参数,无构造参数,重写toString方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-11-11
  • Gradle配置国内镜像加速指南(最全最详细)

    Gradle配置国内镜像加速指南(最全最详细)

    在使用 Gradle 进行 Java、Android 或其他 JVM 项目开发时,最令人头疼的问题之一就是依赖下载速度慢,所以本文将提供最详细最全面的 Gradle 配置国内镜像的指南,希望对大家有所帮助
    2025-08-08
  • java datetime数据类型去掉时分秒的案例详解

    java datetime数据类型去掉时分秒的案例详解

    在Java中,如果我们想要表示一个日期而不包括时间(时分秒),我们通常会使用java.time包中的LocalDate类,这篇文章主要介绍了java datetime数据类型去掉时分秒,需要的朋友可以参考下
    2024-06-06
  • jackson在springboot中的使用方式-自定义参数转换器

    jackson在springboot中的使用方式-自定义参数转换器

    这篇文章主要介绍了jackson在springboot中的使用方式-自定义参数转换器,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-10-10
  • 基于application和bootstrap的加载顺序及区别说明

    基于application和bootstrap的加载顺序及区别说明

    这篇文章主要介绍了application和bootstrap的加载顺序及区别说明,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-07-07
  • 基于XML的Spring声明事务控制

    基于XML的Spring声明事务控制

    这篇文章主要为大家详细介绍了基于XML的Spring声明事务控制,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-07-07
  • springBoot接入阿里云oss的实现步骤

    springBoot接入阿里云oss的实现步骤

    这篇文章主要介绍了springBoot接入阿里云oss的实现步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-01-01
  • SpringBoot结合mockito测试实战

    SpringBoot结合mockito测试实战

    与集成测试将系统作为一个整体测试不同,单元测试更应该专注于某个类。所以当被测试类与外部类有依赖的时候,尤其是与数据库相关的这种费时且有状态的类,很难做单元测试。但好在可以通过“Mockito”这种仿真框架来模拟这些比较费时的类,从而专注于测试某个类内部的逻辑
    2022-11-11
  • Java注解@Conditional与@Profile的使用区别

    Java注解@Conditional与@Profile的使用区别

    这篇文章主要介绍了Java注解@Conditional与@Profile的使用区别,@Profile和@Conditional是Spring提供的两种常用机制,它们可以根据不同的条件动态决定某些Bean是否加载,从而实现环境隔离、模块选择、特性开关等功能,需要的朋友可以参考下
    2025-05-05
  • Java实现字符串与基本数据类型转换的全面指南

    Java实现字符串与基本数据类型转换的全面指南

    本文详细介绍了Java中字符串与基本数据类型之间转换的方法,包括将字符串转换为基本数据类型,以及将基本数据类型转换为字符串的各种技术,有需要的小伙伴可以了解下
    2025-09-09

最新评论