我正在尝试创建自定义ObjectMapper但遇到问题。我想在序列化期间使用Enum的toString方法:
XML配置:
<bean id="restTemplateMS" class="org.springframework.web.client.RestTemplate">
<constructor-arg ref="httpClientFactory" />
<property name="messageConverters">
<list>
<bean
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>somethio</value>
</list>
</property>
<property name="objectMapper" ref="customObjectMapper"/>
</bean>
</list>
</property>
</bean>
<bean id="customObjectMapper" class="com.mag.xxx.CustomJsonObjectMapper"/>
爪哇:
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;
public class CustomJsonObjectMapper extends ObjectMapper
{
public CustomJsonObjectMapper()
{
super();
this.configure(SerializationConfig.Feature.WRITE_ENUMS_USING_TO_STRING, true);
}
}
错误:
Caused by: java.lang.IllegalStateException: Cannot convert value of type [com.mag.xxx.CustomJsonObjectMapper] to required type [org.codehaus.jackson.map.ObjectMapper] for property 'objectMapper': no matching editors or conversion strategy found
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:241)
at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:470)