溫馨提示×

java返回responseentity怎么設置長度

小億
216
2024-05-16 14:09:13
欄目: 編程語言

在Java中,可以通過設置Content-Length頭來指定返回的ResponseEntity的長度。以下是一個示例代碼:

@GetMapping("/example")
public ResponseEntity<String> getExample() {
    String response = "This is an example response";
    
    HttpHeaders headers = new HttpHeaders();
    headers.setContentLength(response.getBytes().length);
    
    return ResponseEntity.ok()
            .headers(headers)
            .body(response);
}

在上面的代碼中,我們首先創建了一個HttpHeaders對象,并調用setContentLength方法來設置返回內容的長度。然后我們將這個HttpHeaders對象傳遞給ResponseEntity.ok().headers(headers).body(response)方法,以指定返回的ResponseEntity的長度。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女