Jackson可以与Spring MVC 3.0一起使用,还可以将RequestBody绑定到Java Bean吗? [英] Can Jackson be used with Spring MVC 3.0 to also bind the RequestBody to a Java Bean?
问题描述
我有一个非常简单的问题,但我似乎无法找到一个简单的答案:
我让Jackson工作序列化 Java bean 使用 @ResponseBody
<响应中的strong>到 JSON 但是我没有设法使用
@RequestBody 序列化 JSON 到 Java bean code>
例如
//这个工作正常,Bean被序列化为JSON
@RequestMapping(...)
public @ResponseBody Bean getSomething(...){
// ... ...
}
//我不知道如何使这项工作,如果有的话是一种方式所有
@RequestMapping(...)
public void setSomething(@RequestBody Bean bean,...){
// ... ...
}
- 请求是否可能?
- 如果是这样,如何配置?
- 是否有JQuery示例/提示(例如设置正确的内容类型)?
更新:
参见 JQuery,Spring MVC @RequestBody和JSON - 使它协同工作配置中的一些怪癖(它适用于ResponseBody,但不适用于RequestBody,它不是没有意义,配置是正确还是错误。可能是一个错误?)
解决方案
-
是的,有可能。
-
如果您的@ResponseBody正在运行,您的服务器端配置可能会很好。
-
您需要将content-type设置为application / json。 JQuery.ajax()方法有一个contentType参数。可以找到AJAX和Spring 3的一个很好的示例/摘要此处。请注意,他使用的是$ .postJSON方法,很可能是这个简单的插件。
I have a very simple question, but I can't seem to find a simple answer:
I got Jackson working to serialize Java beans to JSON in the response using @ResponseBody
But I didn't manage to serialize back JSON to Java beans in the request using @RequestBody
e.g.
// this works fine, Bean is being serialized to JSON
@RequestMapping(...)
public @ResponseBody Bean getSomething(...){
//...
}
// I don't know how to make this work, if if there is a way at all
@RequestMapping(...)
public void setSomething(@RequestBody Bean bean, ...){
//...
}
- Is it possible for the request at all?
- If so, how to configure it?
- Is there a JQuery example / tips (e.g. setting the right content type)?
Update:
See JQuery, Spring MVC @RequestBody and JSON - making it work together some quirks in the configuration (it worked for ResponseBody, but didn't for RequestBody, which doesn't make sense, the configuration is either correct or wrong. could be a bug?)
解决方案
Yes, it is possible.
Your server side configuration is probably fine if your @ResponseBody is working.
You will need to set the content-type to application/json. The JQuery.ajax() method has a contentType parameter. A great example/summary of AJAX and Spring 3 can be found here. Note that he is using a $.postJSON method, which is most likely this simple plugin.
这篇关于Jackson可以与Spring MVC 3.0一起使用,还可以将RequestBody绑定到Java Bean吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!