{"id":17850,"date":"2025-06-20T10:48:56","date_gmt":"2025-06-20T02:48:56","guid":{"rendered":"https:\/\/92it.top\/?p=17850"},"modified":"2025-06-20T10:48:56","modified_gmt":"2025-06-20T02:48:56","slug":"spring-responseentity-%e5%92%8c-responsebody","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=17850","title":{"rendered":"Spring ResponseEntity \u548c ResponseBody"},"content":{"rendered":"\n<p>\u5728 Spring \u6846\u67b6\u4e2d\uff0c<code>ResponseEntity<\/code>\u00a0\u662f\u4e00\u4e2a\u7528\u4e8e\u6784\u5efa HTTP \u54cd\u5e94\u7684\u5f3a\u5927\u5de5\u5177\u7c7b\u3002\u5b83\u5141\u8bb8\u4f60\u5b8c\u5168\u63a7\u5236 HTTP \u54cd\u5e94\u7684\u4e09\u4e2a\u6838\u5fc3\u90e8\u5206\uff1a<strong>\u72b6\u6001\u7801<\/strong>\u3001<strong>\u54cd\u5e94\u5934<\/strong>\u548c<strong>\u54cd\u5e94\u4f53<\/strong>\uff0c\u662f\u6784\u5efa REST API \u65f6\u7684\u5173\u952e\u7ec4\u4ef6\u3002<\/p>\n\n\n\n<p>\u3000\u3000<\/p>\n\n\n\n<p><strong>\u4e00\u3001\u6838\u5fc3\u4f5c\u7528<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<ol>\n<li><strong>\u5c01\u88c5\u5b8c\u6574\u7684 HTTP \u54cd\u5e94<\/strong>\n<ul>\n<li>\u5305\u542b\u72b6\u6001\u7801\uff08\u5982\u00a0<code>200 OK<\/code>\u3001<code>404 Not Found<\/code>\uff09<\/li>\n\n\n\n<li>\u5305\u542b\u54cd\u5e94\u5934\uff08\u5982\u00a0<code>Content-Type<\/code>\u3001<code>Cache-Control<\/code>\uff09<\/li>\n\n\n\n<li>\u5305\u542b\u54cd\u5e94\u4f53\uff08\u8fd4\u56de\u7ed9\u5ba2\u6237\u7aef\u7684\u6570\u636e\uff09<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u66ff\u4ee3\u00a0<code>@ResponseBody<\/code>\u00a0\u7684\u66f4\u7075\u6d3b\u65b9\u5f0f<\/strong>\n<ul>\n<li><code>@ResponseBody<\/code>\u00a0\u53ea\u80fd\u8bbe\u7f6e\u54cd\u5e94\u4f53\uff0c\u65e0\u6cd5\u76f4\u63a5\u63a7\u5236\u72b6\u6001\u7801\u548c\u5934\u4fe1\u606f<\/li>\n\n\n\n<li><code>ResponseEntity<\/code>\u00a0\u53ef\u4ee5\u540c\u65f6\u5b9a\u5236\u4e09\u8005<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<p>\u3000<\/p>\n\n\n\n<p><strong>\u4e8c\u3001\u5173\u952e\u5c5e\u6027\u4e0e\u65b9\u6cd5<\/strong><\/p>\n\n\n\n<p>1.\u00a0<strong>\u72b6\u6001\u7801\uff08HTTP Status Code\uff09<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ \u8fd4\u56de404 Not Found\nreturn ResponseEntity.notFound().build();\n\n\/\/ \u8fd4\u56de201 Created\nreturn ResponseEntity.status(HttpStatus.CREATED).body(newUser);\n\n\n<\/pre>\n\n\n\n<p>\u3000<\/p>\n\n\n\n<p>2.\u00a0<strong>\u54cd\u5e94\u5934\uff08HTTP Headers\uff09<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ \u6dfb\u52a0\u81ea\u5b9a\u4e49\u5934\nHttpHeaders headers = new HttpHeaders();\nheaders.add(\"Location\", \"\/users\/\" + userId);\nreturn ResponseEntity.ok().headers(headers).body(user);<\/pre>\n\n\n\n<p>\u3000<\/p>\n\n\n\n<p>3.\u00a0<strong>\u54cd\u5e94\u4f53\uff08Response Body\uff09<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ \u8fd4\u56deJSON\u683c\u5f0f\u7684\u7528\u6237\u5bf9\u8c61\nreturn ResponseEntity.ok(user);\n\n\/\/ \u8fd4\u56de\u7a7a\u54cd\u5e94\uff08204 No Content\uff09\nreturn ResponseEntity.noContent().build();<\/pre>\n\n\n\n<p>\u3000<\/p>\n\n\n\n<p><strong>\u4e09\u3001\u5e38\u89c1\u7528\u6cd5\u793a\u4f8b<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>1.\u00a0<strong>\u8fd4\u56de\u6210\u529f\u54cd\u5e94\uff08200 OK\uff09<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@GetMapping(\"\/users\/{id}\")\npublic ResponseEntity&lt;User> getUser(@PathVariable Long id) {\n    User user = userService.findById(id);\n    return ResponseEntity.ok(user);\n}<\/pre>\n\n\n\n<p>\u3000<\/p>\n\n\n\n<p>2.\u00a0<strong>\u8fd4\u56de\u521b\u5efa\u6210\u529f\uff08201 Created\uff09<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@PostMapping(\"\/users\")\npublic ResponseEntity&lt;User> createUser(@RequestBody User user) {\n    User savedUser = userService.save(user);\n    return ResponseEntity\n        .created(URI.create(\"\/users\/\" + savedUser.getId()))\n        .body(savedUser);\n}<\/pre>\n\n\n\n<p>\u3000<\/p>\n\n\n\n<p>3.\u00a0<strong>\u8fd4\u56de\u9519\u8bef\u54cd\u5e94\uff08404 Not Found\uff09<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@GetMapping(\"\/users\/{id}\")\npublic ResponseEntity&lt;User> getUser(@PathVariable Long id) {\n    User user = userService.findById(id);\n    if (user == null) {\n        return ResponseEntity.notFound().build();\n    }\n    return ResponseEntity.ok(user);\n}<\/pre>\n\n\n\n<p>\u3000<\/p>\n\n\n\n<p>4.\u00a0<strong>\u8fd4\u56de\u5e26\u81ea\u5b9a\u4e49\u5934\u7684\u54cd\u5e94<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@GetMapping(\"\/download\")\npublic ResponseEntity&lt;Resource> downloadFile() {\n    Resource file = fileService.getFile();\n    return ResponseEntity.ok()\n        .header(HttpHeaders.CONTENT_DISPOSITION, \"attachment; filename=file.txt\")\n        .body(file);\n}<\/pre>\n\n\n\n<p>\u3000<\/p>\n\n\n\n<p><strong>\u56db\u3001\u4e0e\u5176\u4ed6\u6ce8\u89e3\u7684\u5bf9\u6bd4<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><thead><tr><th><strong>\u65b9\u5f0f<\/strong><\/th><th><strong>\u63a7\u5236\u72b6\u6001\u7801<\/strong><\/th><th><strong>\u63a7\u5236\u54cd\u5e94\u5934<\/strong><\/th><th><strong>\u63a7\u5236\u54cd\u5e94\u4f53<\/strong><\/th><th><strong>\u9002\u7528\u573a\u666f<\/strong><\/th><\/tr><\/thead><tbody><tr><td><code>ResponseEntity<\/code><\/td><td>\u2705<\/td><td>\u2705<\/td><td>\u2705<\/td><td>\u9700\u5b8c\u5168\u81ea\u5b9a\u4e49 HTTP \u54cd\u5e94\u7684\u573a\u666f<\/td><\/tr><tr><td><code>@ResponseBody<\/code><\/td><td>\u274c<\/td><td>\u274c<\/td><td>\u2705<\/td><td>\u7b80\u5355\u8fd4\u56de\u5bf9\u8c61\uff0c\u4f7f\u7528\u9ed8\u8ba4\u72b6\u6001\u7801<\/td><\/tr><tr><td><code>@ResponseStatus<\/code><\/td><td>\u2705<\/td><td>\u274c<\/td><td>\u2705<\/td><td>\u56fa\u5b9a\u72b6\u6001\u7801\uff0c\u65e0\u9700\u81ea\u5b9a\u4e49\u54cd\u5e94\u5934<\/td><\/tr><tr><td><code>@RestControllerAdvice<\/code><\/td><td>\u2705<\/td><td>\u2705<\/td><td>\u2705<\/td><td>\u5168\u5c40\u5f02\u5e38\u5904\u7406\uff0c\u7edf\u4e00\u54cd\u5e94\u683c\u5f0f<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u3000<\/p>\n\n\n\n<p><strong>\u4e94\u3001\u8fdb\u9636\u6280\u5de7<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>1.\u00a0<strong>\u94fe\u5f0f\u8c03\u7528\u7b80\u5316\u4ee3\u7801<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">return ResponseEntity\n    .status(HttpStatus.OK)\n    .header(\"Custom-Header\", \"value\")\n    .contentType(MediaType.APPLICATION_JSON)\n    .body(user);<\/pre>\n\n\n\n<p>\u3000<\/p>\n\n\n\n<p>2.\u00a0<strong>\u6cdb\u578b\u652f\u6301<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ \u660e\u786e\u6307\u5b9a\u54cd\u5e94\u4f53\u7c7b\u578b\nResponseEntity&lt;User> response = ResponseEntity.ok(user);\n<\/pre>\n\n\n\n<p>\u3000<\/p>\n\n\n\n<p>3.\u00a0<strong>\u9759\u6001\u5de5\u5382\u65b9\u6cd5<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">ResponseEntity.ok()          \/\/ 200 OK\nResponseEntity.created(uri)  \/\/ 201 Created\nResponseEntity.noContent()   \/\/ 204 No Content\nResponseEntity.badRequest()  \/\/ 400 Bad Request\nResponseEntity.notFound()    \/\/ 404 Not Found<\/pre>\n\n\n\n<p>\u3000<\/p>\n\n\n\n<p><strong>\u516d\u3001\u603b\u7ed3<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><code>ResponseEntity<\/code>&nbsp;\u662f Spring MVC \u4e2d\u6784\u5efa REST API \u7684\u6838\u5fc3\u5de5\u5177\uff0c\u5b83\u8ba9\u4f60\u80fd\u591f\uff1a<\/p>\n\n\n\n<ul>\n<li>\u7cbe\u786e\u63a7\u5236 HTTP \u54cd\u5e94\u7684\u5404\u4e2a\u90e8\u5206<\/li>\n\n\n\n<li>\u5b9e\u73b0\u6807\u51c6\u5316\u7684\u9519\u8bef\u5904\u7406<\/li>\n\n\n\n<li>\u652f\u6301\u590d\u6742\u7684\u54cd\u5e94\u573a\u666f\uff08\u5982\u6587\u4ef6\u4e0b\u8f7d\u3001\u81ea\u5b9a\u4e49\u5934\uff09<\/li>\n<\/ul>\n\n\n\n<p>\u8fc7\u5ea6\u4f7f\u7528\u00a0<code>ResponseEntity<\/code><\/p>\n\n\n\n<ul>\n<li>\u5bf9\u4e8e\u7b80\u5355\u573a\u666f\uff0c\u4f18\u5148\u4f7f\u7528\u00a0<code>@ResponseBody<\/code>\u00a0+\u00a0<code>@ResponseStatus<\/code><\/li>\n\n\n\n<li>\u4ec5\u5728\u9700\u8981\u590d\u6742\u63a7\u5236\u65f6\u4f7f\u7528\u00a0<code>ResponseEntity<\/code><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Spring \u6846\u67b6\u4e2d\uff0cResponseEntity\u00a0\u662f\u4e00\u4e2a\u7528\u4e8e\u6784\u5efa HTTP \u54cd\u5e94\u7684\u5f3a\u5927\u5de5\u5177\u7c7b\u3002\u5b83\u5141\u8bb8\u4f60\u5b8c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26,27],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/17850"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=17850"}],"version-history":[{"count":1,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/17850\/revisions"}],"predecessor-version":[{"id":17851,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/17850\/revisions\/17851"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=17850"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=17850"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=17850"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}