知識庫 / Spring / Spring MVC RSS 訂閱

Spring 請求體和響應體註解

REST,Spring MVC
HongKong
5
03:53 AM · Dec 06 ,2025

1. 引言

本快速教程提供對 Spring 的 @RequestBody@ResponseBody 註解的簡潔概述。

2. @RequestBody

HttpRequest 的請求體映射到傳輸或領域對象,從而啓用對傳入HttpRequest 請求體自動反序列化到 Java 對象。

首先,讓我們來看一個 Spring 控制器方法:

@PostMapping("/request")
public ResponseEntity postController(
  @RequestBody LoginForm loginForm) {
 
    exampleService.fakeAuthenticate(loginForm);
    return ResponseEntity.ok(HttpStatus.OK);
}

Spring 會自動將 JSON 反序列化為 Java 類型,前提是已指定了適當的類型。

默認情況下,使用 @RequestBody 註解指定的數據類型,必須與客户端控制器發送的 JSON 對應。

public class LoginForm {
    private String username;
    private String password;
    // ...
}

在這裏,我們用來表示 HttpRequest 的對象映射到我們的 LoginForm 對象。

我們用 CURL 測試它:

curl -i \
-H "Accept: application/json" \
-H "Content-Type:application/json" \
-X POST --data 
  '{"username": "johnny", "password": "password"}' "https://localhost:8080/spring-boot-rest/post/request"

這是使用 @RequestBody 註解構建 Spring REST API 和 Angular 客户端所需的所有內容。

3. @ResponseBody

@ResponseBody 註解告知控制器,返回的對象將被自動序列化為 JSON 格式,並傳遞到 HttpResponse 對象中。

假設我們有一個自定義 Response 對象:

public class ResponseTransfer {
    private String text; 
    
    // standard getters/setters
}

接下來,可以實現相關的控制器:

@Controller
@RequestMapping("/post")
public class ExamplePostController {

    @Autowired
    ExampleService exampleService;

    @PostMapping("/response")
    @ResponseBody
    public ResponseTransfer postResponseController(
      @RequestBody LoginForm loginForm) {
        return new ResponseTransfer("Thanks For Posting!!!");
     }
}

在我們的瀏覽器開發者控制枱中或使用像Postman這樣的工具中,我們可以看到以下響應:

{"text":"Thanks For Posting!!!"}

請記住,我們無需為帶有註解的控制器添加註解,因為 Spring 默認會處理它。

3.1. 設置內容類型

當使用 <em/>@ResponseBody</em/> 註解時,我們仍然可以顯式地設置方法返回的內容類型。

為此,我們可以使用 <em/>@RequestMapping</em/><em/>produces</em/> 屬性。請注意,諸如@PostMapping</em/>@GetMapping</em/>` 等註解定義了該參數的別名。

現在,讓我們添加一個新的端點,以發送 JSON 響應:

@PostMapping(value = "/content", produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public ResponseTransfer postResponseJsonContent(
  @RequestBody LoginForm loginForm) {
    return new ResponseTransfer("JSON Content!");
}

在示例中,我們使用了 MediaType.APPLICATION_JSON_VALUE 常量。 此外,我們也可以直接使用 application/json

接下來,我們實現一個新的方法,該方法對應相同的 /content 路徑,但返回 XML 內容:

@PostMapping(value = "/content", produces = MediaType.APPLICATION_XML_VALUE)
@ResponseBody
public ResponseTransfer postResponseXmlContent(
  @RequestBody LoginForm loginForm) {
    return new ResponseTransfer("XML Content!");
}

現在,根據請求報頭中發送的 Accept 參數的值,我們將獲得不同的響應。

讓我們看看實際效果:

curl -i \ 
-H "Accept: application/json" \ 
-H "Content-Type:application/json" \ 
-X POST --data 
  '{"username": "johnny", "password": "password"}' "https://localhost:8080/spring-boot-rest/post/content"

CURL 命令返回 JSON 響應:

HTTP/1.1 200
Content-Type: application/json
Transfer-Encoding: chunked
Date: Thu, 20 Feb 2020 19:43:06 GMT

{"text":"JSON Content!"}

現在,讓我們更改 Accept 參數:

curl -i \
-H "Accept: application/xml" \
-H "Content-Type:application/json" \
-X POST --data
  '{"username": "johnny", "password": "password"}' "https://localhost:8080/spring-boot-rest/post/content"

正如預期的那樣,這次我們得到了XML內容:

HTTP/1.1 200
Content-Type: application/xml
Transfer-Encoding: chunked
Date: Thu, 20 Feb 2020 19:43:19 GMT

<ResponseTransfer><text>XML Content!</text></ResponseTransfer>

4. 結論

我們構建了一個簡單的 Angular 客户端,用於 Spring 應用,演示瞭如何使用 @RequestBody@ResponseBody 註解。

此外,我們還展示瞭如何設置內容類型,使用 @ResponseBody 時。

user avatar
0 位用戶收藏了這個故事!
收藏

發佈 評論

Some HTML is okay.