开发者

Can Jackson be used with Spring MVC 3.0 to also bind the RequestBody to a Java Bean?

开发者 https://www.devze.com 2023-03-04 17:16 出处:网络
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

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, ...){
    //...
}
  1. Is it possible for the request at all?
  2. If so, how to configure it?
  3. Is there a JQuery example / tips (e.g. setting the right content type)?

Update:

See JQuery, Spring MVC @RequestBody and JSON - making it wo开发者_如何学JAVArk 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?)


  1. Yes, it is possible.

  2. Your server side configuration is probably fine if your @ResponseBody is working.

  3. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消