Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I could add additional header on Response Body

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 for this action/specific action method for specific controller. not common for all JSON @ResponseBody action method .

@RequestMapping(value="ajaxDenied", method = RequestMethod.GET) public @ResponseBody Map<String, Object> ajaxDenied(ModelMap model) {      Map<String, Object> message = new HashMap<String, Object>();     message.put("severity", "error");     message.put("summary", "Restricted access only");     message.put("code", 200);      Map<String, Object> json = new HashMap<String, Object>();     json.put("success", false);     json.put("message", message);      return json; } 

In the different way I could add additional headers as my demand but here is some problem in generating pure JSON. Its generate buggy JSON and able to parse few browser.

@RequestMapping(value="ajaxSuccess", method = RequestMethod.GET) public ResponseEntity<String> ajaxSuccess(){     Map<String, Object> message = new HashMap<String, Object>();      message.put("severity", "info");     message.put("location", "/");     message.put("summary", "Authenticated successfully.");     message.put("code", 200);      Map<String, Object> json = new HashMap<String, Object>();     json.put("success", true);     json.put("message", message);      String data = "";     try {         ObjectMapper mapper = new ObjectMapper();         data  = mapper.writeValueAsString(json);     } catch (Exception e) { //TODO     }     HttpHeaders headers = new HttpHeaders();     headers.add("Content-Type", "application/json; charset=UTF-8");     headers.add("X-Fsl-Location", "/");     headers.add("X-Fsl-Response-Code", "302");     return (new ResponseEntity<String>(data, headers, HttpStatus.OK)); } 

this action method could generate JSON String with escape character rather than pure JSON so depend on browser how it will be parse, Its cause failure for chrome. The output just look like

"{\"message\":{\"summary\":\"Authenticated successfully.\",\"location\":\"/\",\"severity\":\"info\",\"code\":\"200\"},\"success\":true}" 

but our desired output

{   "message":{     "summary": "Authenticated successfully.",     "location":"/",     "severity":"info",     "code":"200"   },   "success":true } 

I want to generate pure JSON with additional headers based on conditions for specific action of specific controller.

like image 672
Śhāhēēd Avatar asked Nov 14 '13 05:11

Śhāhēēd


People also ask

How do I add a header to a response?

Select the web site where you want to add the custom HTTP response header. In the web site pane, double-click HTTP Response Headers in the IIS section. In the actions pane, select Add. In the Name box, type the custom HTTP header name.

Can we change response header?

You can add a new header or remove, modify an existing request/response header. For this example, we will select Remove. Add operation works when a new header is to be added to the request/response whereas modify option modifies an already existing header.

What are response headers?

A response header is an HTTP header that can be used in an HTTP response and that doesn't relate to the content of the message. Response headers, like Age , Location or Server are used to give a more detailed context of the response.


2 Answers

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); } 
like image 159
vargen_ Avatar answered Sep 18 '22 08:09

vargen_


Here is the solution as the suggestion of M. Deinum

@RequestMapping(value="ajaxSuccess", method = RequestMethod.GET) public ResponseEntity<Map<String, Object>> ajaxSuccess(){     Map<String, Object> message = new HashMap<String, Object>();      message.put("severity", "info");     message.put("location", "/");     message.put("summary", "Authenticated successfully.");     message.put("code", 200);      Map<String, Object> json = new HashMap<String, Object>();     json.put("success", true);     json.put("message", message);      HttpHeaders headers = new HttpHeaders();     headers.add("Content-Type", "application/json; charset=UTF-8");     headers.add("X-Fsl-Location", "/");     headers.add("X-Fsl-Response-Code", "302");     return (new ResponseEntity<Map<String, Object>>(json, headers, HttpStatus.OK)); } 
like image 29
Śhāhēēd Avatar answered Sep 21 '22 08:09

Śhāhēēd