2012-07-09 85 views
2

我在我的Spring應用程序上配置傑克遜很困難。我可以讓它工作,但它似乎不接受任何配置。基本上我試圖實現的是有一個ObjectMapper掃描Spring格式註釋。傑克遜+ Spring3.0.5自定義對象映射器

什麼,我想是這樣的:

<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]類型的值轉換爲required爲屬性'objectMapper'鍵入[org.codehaus.jackson.map.ObjectMapper]:找不到匹配的編輯器或轉換策略 at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:241) at org.springframework.beans .BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:470) ...... 55多個

我嘗試切換到普通視圖解析器,只是其在AnnotationMethodHandler對象映射,以及關於作品服務器啓動,但它只是完全忽略了任何配置。

對於下一步該做什麼,我完全不知所措。

感謝您的任何幫助。

回答

1

更新:

<property name="objectMapper" value="jacksonObjectMapper" /> 

有:

<property name="objectMapper" ref="jacksonObjectMapper" /> 

裁判沒有價值。