【发布时间】:2013-11-29 22:51:25
【问题描述】:
我希望能够方便地将对象自动序列化为 JSON,并能够返回原始 JSON 字符串。我正在使用 Gson 而不是 Jackson,因为 Gson 在我的应用程序中已经存在了一段时间,并且我的应用程序中遍布现有的调整、转换器和注释。
<mvc:annotation-driven >
<mvc:message-converters register-defaults="true">
<bean class="com.test.GSONHttpMessageConverter" />
</mvc:message-converters>
</mvc:annotation-driven>
我可以自动序列化pojo的:
@RequestMapping(value="foo/{name}", method = RequestMethod.GET)
public @ResponseBody Shop getShopInJSON(@PathVariable String name) {
return new Shop();
}
我希望它也能正常工作:
@RequestMapping(value="rawJsonTest/{name}", method = RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody String rawJsonTest(@PathVariable String name) {
return "{\"test\":5}";
}
现在的结果是一个转义值:
"{\"test\":5}"
代替:
{"test":5}
【问题讨论】:
标签: json spring-mvc gson