3

我对杰克逊 2.1 有疑问。

我的 pojo 有一些日期属性,我想把它变成字符串,我在 spring-servlet.xml 中设置了它,但它没有用。

我不喜欢@JsonSerialize(using = JsonDateSerializer.class)在二传手上使用。

这是我的配置:

<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
    <property name="objectMapper">
        <bean class="com.fasterxml.jackson.databind.ObjectMapper">
            <property name="dateFormat">
                <bean class="java.text.SimpleDateFormat">
                    <constructor-arg type="java.lang.String" value="yyyy-MM-dd"></constructor-arg>
                </bean>
            </property>
        </bean>
    </property>
</bean>
4

2 回答 2

4

假设您使用的是 Spring 3.1,您应该自定义mvc-annotation driven标签属性,如 在 Spring 中配置 ObjectMapper 中所示

假设您的 bean 声明是正确的,我认为它应该类似于

<mvc:annotation-driven>
        <mvc:message-converters register-defaults="true">
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
              <property name="objectMapper">
                 <bean class="com.fasterxml.jackson.databind.ObjectMapper">
                     <property name="dateFormat">
                     <bean class="java.text.SimpleDateFormat">
                     <constructor-arg type="java.lang.String" value="yyyy-MM-dd"></constructor-arg>
                     </bean>
                     </property>
                 </bean>
              </property>
        </bean>
    </mvc:message-converters>

    </mvc:annotation-driven>
于 2012-10-27T10:03:47.657 回答
1

我的设置是:

<!-- Date Format -->
<bean id="dateFormatter" class="java.text.SimpleDateFormat">
    <constructor-arg value="yyyy-MM-dd"/>
</bean>

<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="targetObject" ref="dateFormatter" />
    <property name="targetMethod" value="setTimeZone" />
    <property name="arguments">
        <list>
            <ref bean="timeZone"/>
        </list>
    </property>
</bean>
<!-- End Date Format -->

<!-- Jackson Object Mapper -->
<bean id="jacksonObjectMapper" class="org.codehaus.jackson.map.ObjectMapper"/>
 <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="targetObject" ref="jacksonObjectMapper" />
    <property name="targetMethod" value="configure" />
    <property name="arguments">
        <list>
             <value type="org.codehaus.jackson.map.DeserializationConfig.Feature">FAIL_ON_UNKNOWN_PROPERTIES</value>
            <value>false</value>
        </list>
    </property>
</bean>

<bean id="jacksonDeserializationConfig" class="org.codehaus.jackson.map.DeserializationConfig" factory-bean="jacksonObjectMapper" factory-method="getDeserializationConfig" />

<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="targetObject" ref="jacksonDeserializationConfig" />
    <property name="targetMethod" value="setDateFormat" />
    <property name="arguments">
        <list>
            <ref bean="dateFormatter"/>
        </list>
    </property>
</bean>

<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="targetObject" ref="jacksonObjectMapper" />
    <property name="targetMethod" value="setDeserializationConfig" />
    <property name="arguments">
        <list>
           <ref bean="jacksonDeserializationConfig"/>
        </list>
    </property>
</bean>
<!-- End Jackson Object Mapper -->



<!-- JSON provider -->
<bean id="jsonRestProvider" class="org.codehaus.jackson.jaxrs.JacksonJsonProvider">
    <property name="mapper" ref="jacksonObjectMapper"/>
</bean>
于 2014-07-10T19:09:09.930 回答