Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I could add additional header on Response Body
    primarykey
    data
    text
    <p>Just see the code snippet of <code>SpringMVC-3.2.x</code> controller action method. Its quite easy to generate <code>JSON</code> but unable to add addtional custom header only for this action/specific action method for specific controller. not common for all <code>JSON</code> <code>@ResponseBody</code> action method .</p> <pre><code>@RequestMapping(value="ajaxDenied", method = RequestMethod.GET) public @ResponseBody Map&lt;String, Object&gt; ajaxDenied(ModelMap model) { Map&lt;String, Object&gt; message = new HashMap&lt;String, Object&gt;(); message.put("severity", "error"); message.put("summary", "Restricted access only"); message.put("code", 200); Map&lt;String, Object&gt; json = new HashMap&lt;String, Object&gt;(); json.put("success", false); json.put("message", message); return json; } </code></pre> <p>In the different way I could add additional headers as my demand but here is some problem in generating pure <code>JSON</code>. Its generate buggy <code>JSON</code> and able to parse few browser.</p> <pre><code>@RequestMapping(value="ajaxSuccess", method = RequestMethod.GET) public ResponseEntity&lt;String&gt; ajaxSuccess(){ Map&lt;String, Object&gt; message = new HashMap&lt;String, Object&gt;(); message.put("severity", "info"); message.put("location", "/"); message.put("summary", "Authenticated successfully."); message.put("code", 200); Map&lt;String, Object&gt; json = new HashMap&lt;String, Object&gt;(); 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&lt;String&gt;(data, headers, HttpStatus.OK)); } </code></pre> <p>this action method could generate <code>JSON</code> String with escape character rather than pure <code>JSON</code> so depend on browser how it will be parse, Its cause failure for chrome. The output just look like</p> <pre><code>"{\"message\":{\"summary\":\"Authenticated successfully.\",\"location\":\"/\",\"severity\":\"info\",\"code\":\"200\"},\"success\":true}" </code></pre> <p>but our desired output</p> <pre><code>{ "message":{ "summary": "Authenticated successfully.", "location":"/", "severity":"info", "code":"200" }, "success":true } </code></pre> <p>I want to generate pure <code>JSON</code> with additional headers based on conditions for specific action of specific controller.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload