2011-11-30 80 views
3

我在使用Spring的内置JSON消耗功能时使用Jackson和REST Web服务有些困难。如果我定义如下:不使用JSON的Spring REST服务

... 
@RequestMapping(value="/stub") 
public void doSomething(@RequestBody User user) { 
    System.err.println("In method"); 
    ... 
} 

......它永远不会达到该方法。我在课堂上有杰克逊。但是,当我手动使用杰克逊:

@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) {...} 
} 

...它完美的作品。 User对象使用所有正确的值正确创建,所以我知道JSON是正确的。它与解码有什么关系?据我所知,从Spring 2.5开始默认解码。也许我错过了其他的东西,也许是一个配置步骤。我web.xml如下:

<?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> 

...而我servlet-context.xml定义:提前

<?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> 

感谢。

+0

我会建议将日志级别设置到调试的弹簧。如果它没有找到控制器,它会告诉你提示出了什么问题。 –

回答

8

我相信如果杰克逊在classpath中,标签会自动设置Jackson。不需要其他配置。

看看添加正确的消耗,并产生与-在RequestMapping:

@RequestMapping(method = RequestMethod.POST, value = "/stub", consumes = "application/json", produces = "application/json") 

这些annoatations是从Spring MVC的3.1项目拉动。您可能需要用户header="",然后才需要正确的MVC标头。

我发现版本3+在这个舞台上有一些巨大的改进。

你可能有两个问题之一:

  1. 杰克逊没有被发现
  2. 映射到控制器配置不正确