我写这样我的春节,REST服务:JSON响应具有反斜杠字符
@RequestMapping(value = "/{name}", method = RequestMethod.GET,
consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Map<String, Object>>
getStudentByName(@PathVariable("name") String name) {
Map<String, Object> map = null;
try {
map = new HashMap<String, Object>();
map.put("name", "Tom Joe);
map.put("id", "t100");
map.put("dept", "nuclear energy");
} catch (Exception e) {
LOG.error("Error finding with name " + name, e);
}
return new ResponseEntity<Map<String, Object>>(map, HttpStatus.OK);
}
我得到这样的输出在客户端基本上是一个字符串:
"{\"name\":\"Tom Joe\",\"id\":\"100\",\"dept\":\"nuclear energy\"}"
但我需要的输出是:
{
"name":"Tom Joe",
"id":"t100",
"dept":"nuclear energy"
}
谁能告诉我如何避免发送和反斜杠反而发送适当的json?我能否做到这一点返回地图?
我会尝试生产= MediaType.APPLICATION_JSON,而MIME类型APPLICATION_JSON_VALUE给你一个字符串转换。 – Tim
@Tim那不会编译。 MediaType.APPLICATION_JSON是一个MediaType值。 'produce'期望'String []'。 –
不知道为什么我的下面的答案是downvoted,但代码似乎工作正常..我会检查您使用的客户端,如果它是一个Java客户端它可能会将转义字符添加到响应时显示它。 – Pete