2017-07-17 25 views

回答

2

,我建議你在文檔中詢問這裏之前搜索:

@RequestMapping("/handle") 
public ResponseEntity<String> handle() { 
    URI location = ...; 
    HttpHeaders responseHeaders = new HttpHeaders(); 
    responseHeaders.setLocation(location); 
    responseHeaders.set("MyResponseHeader", "MyValue"); 
    return new ResponseEntity<String>("Hello World", responseHeaders, HttpStatus.CREATED); 
} 

來源:https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/http/ResponseEntity.html

+0

無法理解其中這些方法應該是目前ResponseEntity表現出一定的升值。我正在關注此頁面以創建服務https://spring.io/guides/gs/rest-service/,並且需要讓響應消息包含標題'Access-Control-Allow-Origin'。 – xpioneer

+0

而不是返回一個「問候」對象,返回一個ResponseEntity,其中的問候對象作爲正文,還可以將標題添加到響應實體。看看可用的構造函數。 –

2

不像其他的答案,使用HttpServletResponse。如果你能避免它,你不想使用低級的Servlet API。返回ResponseEntityHttpEntity

HttpHeaders headers = new HttpHeaders(); 
headers.add("1", "uno"); 

return new ResponseEntity<>(headers, HttpStatus.OK); 
相關問題