這個使用Spring的可以實現的MethodInvokingFactoryBean:
<!-- Jackson 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>
這可以連接到一個RestTemplate這樣的:
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="objectMapper" ref="jacksonObjectMapper" />
</bean>
</list>
</property>
</bean>
它也可以直接注入消息轉換器的使用與Spring MVC:
<mvc:annotation-driven>
<mvc:message-converters>
<!-- Jackson converter for HTTP messages -->
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="objectMapper" ref="jacksonObjectMapper" />
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
不知道的區別是什麼,但我使用com.fasterxml.jackson.databind.ObjectMapper,因此不得不用com.fasterxml.jackson.databind.DeserializationFeature。如果仔細查看jacksonObjectMapper bean及其關聯的MethodInvokingFactoryBean,則稍有不同。 –
對於傑克遜V2轉換應該是定義的消息:org.springframework.http.converter.json.MappingJackson2HttpMessageConverter – svlada