How do I could add additional header on Response Body

后端 未结 3 734
离开以前
离开以前 2021-02-02 11:08

Just see the code snippet of SpringMVC-3.2.x controller action method. Its quite easy to generate JSON but unable to add addtional custom header only f

3条回答
  •  后悔当初
    2021-02-02 11:49

    You can add headers to the ResponseEntity builder. I think it is cleaner this way.

    import org.springframework.http.HttpHeaders;
    import org.springframework.http.ResponseEntity;
    
    // ...
    
    @GetMapping("/my/endpoint")
    public ResponseEntity myEndpointMethod() {
    
        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.CONTENT_TYPE, "application/json; charset=UTF-8");
    
        return ResponseEntity.ok()
                .headers(headers)
                .body(data);
    }
    

提交回复
热议问题