如何在响应正文上添加其他标题 [英] How do I could add additional header on Response Body

查看:117
本文介绍了如何在响应正文上添加其他标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仅请参见SpringMVC-3.2.x控制器操作方法的代码段.生成JSON非常容易,但是仅能针对特定控制器的此操作/特定操作方法添加其他自定义标头.并非所有JSON @ResponseBody动作方法都通用.

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;
}

以不同的方式,我可以根据需要添加其他标头,但这在生成纯JSON时存在一些问题.它会生成错误的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));
}

此操作方法可能生成带有转义字符而不是纯JSONJSON字符串,因此取决于浏览器如何对其进行解析,这会导致chrome失败.输出看起来就像

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}"

但是我们想要的输出

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

我想根据特定控制器的特定操作的条件生成带有其他标头的纯JSON.

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

推荐答案

以下是 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));
}

这篇关于如何在响应正文上添加其他标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆