spring-mvc中将json解析为java对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3921736/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Parsing json into java objects in spring-mvc
提问by oksayt
I'm familiar with how to return json from my @Controller
methods using the @ResponseBody
annotation.
我熟悉如何@Controller
使用@ResponseBody
注释从我的方法中返回 json 。
Now I'm trying to read some json arguments into my controller, but haven't had luck so far. Here's my controller's signature:
现在我正在尝试将一些 json 参数读入我的控制器,但到目前为止还没有运气。这是我的控制器的签名:
@RequestMapping(value = "/ajax/search/sync")
public ModelAndView sync(@RequestParam("json") @RequestBody SearchRequest json) {
But when I try to invoke this method, spring complains that:
Failed to convert value of type 'java.lang.String' to required type 'com.foo.SearchRequest'
但是当我尝试调用这个方法时,spring 抱怨说:
Failed to convert value of type 'java.lang.String' to required type 'com.foo.SearchRequest'
Removing the @RequestBody
annotation doesn't seem to make a difference.
删除@RequestBody
注释似乎没有什么区别。
Manually parsing the json works, so Hymanson must be in the classpath:
手动解析 json 有效,因此 Hymanson 必须在类路径中:
// This works
@RequestMapping(value = "/ajax/search/sync")
public ModelAndView sync(@RequestParam("json") String json) {
SearchRequest request;
try {
request = objectMapper.readValue(json, SearchRequest.class);
} catch (IOException e) {
throw new IllegalArgumentException("Couldn't parse json into a search request", e);
}
Any ideas? Am I trying to do something that's not supported?
有任何想法吗?我是否正在尝试做一些不受支持的事情?
采纳答案by skaffman
Your parameter should either be a @RequestParam
, ora @RequestBody
, not both.
您的参数要么是一个@RequestParam
,或者一个@RequestBody
,不能同时使用。
@RequestBody
is for use with POST and PUT requests, where the body of the request is what you want to parse. @RequestParam
is for named parameters, either on the URL or as a multipart form submission.
@RequestBody
用于 POST 和 PUT 请求,其中请求的正文是您要解析的内容。@RequestParam
用于命名参数,无论是在 URL 上还是作为多部分表单提交。
So you need to decide which one you need. Do you really want to have your JSON as a request parameter? This isn't normally how AJAX works, it's normally sent as the request body.
所以你需要决定你需要哪一个。你真的想把你的 JSON 作为请求参数吗?这通常不是 AJAX 的工作方式,它通常作为请求正文发送。
Try removing the @RequestParam
and see if that works. If not, and you really are posting the JSON as a request parameter, then Spring won't help you process that without additional plumbing (see Customizing WebDataBinder initialization).
尝试删除它@RequestParam
,看看是否有效。如果没有,并且您确实将 JSON 作为请求参数发布,那么如果没有额外的管道,Spring 将无法帮助您处理它(请参阅自定义 WebDataBinder 初始化)。
回答by rafael lean ansay
if you are using jquery on the client side, this worked for me:
如果您在客户端使用 jquery,这对我有用:
Java:
爪哇:
@RequestMapping(value = "/ajax/search/sync")
public ModelAndView sync(@RequestBody SearchRequest json) {
Jquery (you need to include Douglas Crockford's json2.js to have the JSON.stringify function):
Jquery(您需要包含 Douglas Crockford 的 json2.js 才能拥有 JSON.stringify 函数):
$.ajax({
type: "post",
url: "sync", //your valid url
contentType: "application/json", //this is required for spring 3 - ajax to work (at least for me)
data: JSON.stringify(jsonobject), //json object or array of json objects
success: function(result) {
//do nothing
},
error: function(){
alert('failure');
}
});