2013-01-15 37 views
40

傑克遜有註釋使用忽略類中的未知屬性:你如何在全球範圍內設置傑克遜忽略Spring中的未知屬性?

@JsonIgnoreProperties(ignoreUnknown = true) 

它可以讓你使用這個註釋忽略特定屬性:

@JsonIgnore 

如果你想全局設置它,你可以修改對象映射:

// jackson 1.9 and before 
objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); 
// or jackson 2.0 
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); 

如何設置這個全球使用彈簧,其可以在@Autowired服務器啓動時無需編寫其他類?

回答

34

這個使用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> 
+1

不知道的區別是什麼,但我使用com.fasterxml.jackson.databind.ObjectMapper,因此不得不用com.fasterxml.jackson.databind.DeserializationFeature。如果仔細查看jacksonObjectMapper bean及其關聯的MethodInvokingFactoryBean,則稍有不同。 –

+1

對於傑克遜V2轉換應該是定義的消息:org.springframework.http.converter.json.MappingJackson2HttpMessageConverter – svlada

18

對於較新的傑克遜版本(2.x),有幾個ch ANGES:

<!-- Jackson Mapper --> 
<bean id="jacksonObjectMapper" class="com.fasterxml.jackson.databind.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="com.fasterxml.jackson.databind.DeserializationFeature">FAIL_ON_UNKNOWN_PROPERTIES</value> 
      <value>false</value> 
     </list> 
    </property> 
</bean> 
+1

在Java代碼,它是這樣的: \t \t objectMapper.configure(com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,假); –

30

傑克遜1.9倍或以下,可以忽略與對象映射器提供商

@Provider 
@Component 
public class JerseyObjectMapperProvider implements ContextResolver<ObjectMapper> { 

    @Override 
    public ObjectMapper getContext(Class<?> type) { 

     ObjectMapper result = new ObjectMapper(); 
     result.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); 
     return result; 
    } 
} 

傑克遜2.x和上面可以忽略與對象映射器提供者未知屬性未知屬性

@Provider 
@Component 
public class JerseyObjectMapperProvider implements ContextResolver<ObjectMapper> { 

    @Override 
    public ObjectMapper getContext(Class<?> type) { 

     ObjectMapper result = new ObjectMapper(); 
     result.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); 
     return result; 
    } 
} 

Jersey類不是由Spring自動發現的。必須手動註冊它們。

public class JerseyConfig extends ResourceConfig { 
    public JerseyConfig() { 
     register(JerseyObjectMapperProvider.class); 
    } 
} 
+1

注意:要在Spring Boot中使用這個Jersey配置,您還需要手動註冊: '/ ** * Jersey類不是由Spring自動發現的。必須手動註冊它們。 */ @命名爲 公共類JerseyConfig擴展了ResourceConfig公共JerseyConfig(){ this.register(JerseyObjectMapperProvider.class); } }' – Alex

+0

感謝您的建議@Alex – xdebug