Spring REST服务不消耗JSON

时间:2011-11-30 06:54:13

标签: spring spring-mvc

使用带有REST Web服务的Jackson使用Spring的内置JSON消费时遇到了一些困难。如果我定义以下内容:

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

提前致谢。

1 个答案:

答案 0 :(得分:8)

我相信如果杰克逊在班级路径中,标签会自动设置杰克逊。无需其他配置。

查看添加正确的消耗并使用RequestMapping生成:

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

这些annoatations是从Spring MVC 3.1项目中提取的。您可能需要使用header="",然后使用正确的MVC标头。

我发现版本3+在这个领域有一些巨大的改进。

您可能遇到以下两个问题之一:

  1. 杰克逊未被发现
  2. 未正确配置控制器的映射