I would like to have a bidirectional JSON to Java serialization
I'm using successfully the Java to JSON to JQuery path... (@ResponseBody
)
e.g.
@RequestMapping(value={"/fooBar/{id}"}, method=RequestMethod.GET)
public @ResponseBody FooBar getFooBar(
@PathVariable String id,
HttpServletResponse response , ModelMap model) {
response.setContentType("application/json");
...
}
and In JQuery I use
$.getJSON('fooBar/1', function(data) {
//do something
});
this works well (e.g. annotations work already, thanks to all the answerers)
However, how do I do the reverse path: have JSON be serialized to a Java Object back using RequestBody?
no matter what I try, I can't get something like this to work:
@RequestMapping(value={"/fooBar/save"}, method=RequestMethod.POST)
public String saveFooBar(@RequestBody FooBar fooBar,
HttpServletResponse response , ModelMap model) {
//This method is never called. (it does when I remove the RequestBody...)
}
I have Jackson configured correctly (it serializes on the way out) and I have MVC set as annotations driven of course
How do I make it work? is it possible at all? or is Spring / JSON / JQuery is oneway (out)?
Update:
I changed this Jackson setting
<bean id="jsonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
<!-- Bind the return value of the Rest service to the ResponseBody. -->
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<util:list id="beanList">
<ref bean="jsonHttpMessageConverter" />
<!-- <ref bean="xmlMessageConverter" /> -->
</util:list>
</property>
</bean>
To the (almost similiar one) suggested
<bean id="jacksonMessageConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jacksonMessageConverter" />
</list>
</property>
</bean>
And it seems to work! I don't know what exactly did the trick, but it works...
In addition to the answers here...
if you are using jquery on the client side, this worked for me:
Java:
Jquery (you need to include Douglas Crockford's json2.js to have the JSON.stringify function):
In Addition you also need to be sure that you have
in your SPring configuration xml.
I also would recommend you to read this blog post. It helped me alot. Spring blog - Ajax Simplifications in Spring 3.0
Update:
just checked my working code where I have
@RequestBody
working correctly. I also have this bean in my config:May be it would be nice to see what
Log4j
is saying. it usually gives more information and from my experience the@RequestBody
will fail if your request's content type is notApplication/JSON
. You can run Fiddler 2 to test it, or even Mozilla Live HTTP headers plugin can help.In case you are willing to use Curl for the calls with JSON 2 and Spring 3.2.0 in hand checkout the FAQ here. As AnnotationMethodHandlerAdapter is deprecated and replaced by RequestMappingHandlerAdapter.
I'm pretty sure you only have to register
MappingJacksonHttpMessageConverter
(the easiest way to do that is through
<mvc:annotation-driven />
in XML or@EnableWebMvc
in Java)See:
Here's a working example:
Maven POM
in folder src/main/webapp/WEB-INF
web.xml
json-servlet.xml
in folder src/main/resources:
mvc-context.xml
In folder src/main/java/test/json
TestController.java
Request.java
Result.java
You can test this setup by executing
mvn jetty:run
on the command line, and then sending a POST request:I used the Poster Firefox plugin to do this.
Here's what the response looks like:
If you do not want to configure the message converters yourself, you can use either @EnableWebMvc or <mvc:annotation-driven />, add Jackson to the classpath and Spring will give you both JSON, XML (and a few other converters) by default. Additionally, you will get some other commonly used features for conversion, formatting and validation.