I am having some difficulty using Spring's built-in JSON consumption using Jackson with REST web services. If I define the following:
...
@RequestMapping(value="/stub")
public void doSomething(@RequestBody User user) {
System.err.println("In method");
...
}
... it never reaches the method. I have Jackson in the classpath. However, when I manually use Jackson:
@RequestMapping(value="/stub")
public void doSomething(@RequestBody String user) {
System.err.println("In method");
User newUser = null;
try {
user = URLDecoder.decode(user, "UTF-8");
} catch (UnsupportedEncodingException e1) {...}
try {
newUser = new ObjectMapper().readValue(user, User.class);
} catch (Exception e) {...}
}
... it works perfectly. The User object gets created correctly with all the correct values, so I know the JSON is correct. Does it have something to do with the decoding? As far as I know from Spring 2.5 the decoding is on by default. Maybe I am missing something else, maybe a configuration step. My web.xml is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="
http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
... while my servlet-context.xml defines:
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
<import resource="mongo-context.xml"/>
<context:component-scan base-package="com.moonlight42.sampleserver.model" />
<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="jsonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="prefixJson" value="false"/>
<property name="supportedMediaTypes" value="application/json"/>
</bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<util:list id="beanList">
<ref bean="jsonHttpMessageConverter"/>
</util:list>
</property>
</bean>
</beans>
Thanks in advance.
I believe that tag setups Jackson automatically if Jackson is in the classpath. No need for other configuration.
Look at adding the correct consumes and produces with-in the RequestMapping:
@RequestMapping(method = RequestMethod.POST, value = "/stub", consumes = "application/json", produces = "application/json")
These annoatations are pulled from a Spring MVC 3.1 project. You may need to user header="" and then the correct MVC header.
I have found that versions 3+ have some huge improvements in this arena.
You may have one of two problems:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With