使用 spring mvc 3 时是否可以禁用自动序列化?
我们已改用 spring mvc,但序列化已经完成,因此我们不想使用自动序列化。是否可以禁用该功能并使 spring 按原样返回字符串?
@RequestMapping(value = "/test", method = RequestMethod.GET)
@ResponseBody
public ResponseEntity<String> justReturnMyResponse() {
return new ResponseEntity<String>(getAllreadySerializedObject(), HttpStatus.OK);;
}
String getAllreadySerializedObject(){
// Some special serialization
....
return jsonizedString;
}
上面的代码再次序列化了字符串,这并不一定是因为它已经被序列化了。
最佳答案
好吧,经过一些测试和阅读,我确实找到了解决方案或其他东西,但我想所有那些纯粹主义者都会痛苦地尖叫。问题是 spring 自动设置 @RequestMapping(products="application/json")
但如果我更改为 @RequestMapping(products="plain/text")
它无需进行两次反序列化即可工作。
这是经过我修复的新代码:
@RequestMapping(value = "/test", method = RequestMethod.GET, produces = "plain/text")
@ResponseBody
public ResponseEntity<String> justReturnMyResponse() {
return new ResponseEntity<String>(getAllreadySerializedObject(), HttpStatus.OK);;
}
String getAllreadySerializedObject(){
// Some special serialization
....
return jsonizedString;
}
关于java - 禁用 Spring mvc 自动序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19997909/