我很难在Spring应用程序上配置Jackson。我可以让它工作,但它似乎不接受任何类型的配置。基本上我想要实现的是有一个ObjectMapper,它可以扫描Spring格式的注释。
我正在尝试的是:
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<bean
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="objectMapper" ref="jacksonObjectMapper" />
</bean>
</list>
</property>
</bean>
<bean id="jacksonObjectMapper" class="ro.softwin.cnfp.ConversionServiceAwareObjectMapper" />
<bean
class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="mediaTypes">
<map>
<entry key="json" value="application/json" />
<entry key="html" value="text/html" />
</map>
</property>
<property name="viewResolvers">
<list>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</list>
</property>
<property name="defaultViews">
<list>
<bean
class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
<property name="prefixJson" value="false" />
<property name="objectMapper" value="jacksonObjectMapper" />
</bean>
</list>
</property>
</bean>
映射器的代码是:
public class ConversionServiceAwareObjectMapper extends ObjectMapper {
@Autowired
public ConversionServiceAwareObjectMapper(
ConversionService conversionService) {
AnnotationIntrospector introspector = AnnotationIntrospector.pair(
new FormatAnnotationIntrospector(conversionService),
DEFAULT_ANNOTATION_INTROSPECTOR);
this.setDeserializationConfig(this.getDeserializationConfig()
.withAnnotationIntrospector(introspector));
this.setSerializationConfig(this.getSerializationConfig()
.withAnnotationIntrospector(introspector).withSerializationInclusion(JsonSerialize.Inclusion.NON_NULL));
}
}
初始化服务器时,会发生以下错误:
java.lang.IllegalStateException:无法将类型[java.lang.String]的值转换为属性“objectMapper”所需的类型[org.codehaus.jackson.map.ObjectMapper]:找不到匹配的编辑器或转换策略 在org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:241) 在org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:470) ......还有55个
我尝试切换到普通视图解析器并在AnnotationMethodHandler中使用对象映射器,这对于服务器启动有效,但它完全忽略了任何配置。
我完全不知道接下来要做什么。
感谢您的帮助。
答案 0 :(得分:1)
更新:
<property name="objectMapper" value="jacksonObjectMapper" />
使用:
<property name="objectMapper" ref="jacksonObjectMapper" />
ref ref not value。