@JsonIgnoreProperties(ignoreUnknown = false)在Spring 4.2.0和更高版本中不起作用

时间:2016-12-13 19:00:45

标签: spring spring-mvc jackson2 jackson-dataformat-xml

@JsonIgnoreProperties(ignoreUnknown = false)无法使用spring 4.2.0和spring的高级版本。但它正在使用4.0.4和4.0.1。 我正在使用spring 4.2.8并且使用了Jackson依赖项

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.6.3</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.6.3</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.6.3</version>
</dependency>

如果我发送带有无效字段的json请求,则它接受作为有效请求。但它应该作为回应给出错误的请求。 例如:如果我有课

public class Student{ 
    private String id; 
    private String name; 
}

如果发送有效的相应json请求,它应该像

{ 
   "id": "123", 
   "name": "test" 
}

但即使我发送json请求和下面的无效字段,它仍然接受。

{ 
    "id": "123", 
    "name": "test", 
    "anyinvalidkey": "test" 
}

但它应该将错误的请求作为回应

4 个答案:

答案 0 :(得分:2)

基于Aarya答案的基于注释的解决方案可以通过以下方式完成:

@Configuration
public class Config implements InitializingBean {

    @Autowired
    private RequestMappingHandlerAdapter converter;

    @Override
    public void afterPropertiesSet() throws Exception {
        configureJacksonToFailOnUnknownProperties();
    }

    private void configureJacksonToFailOnUnknownProperties() {
        MappingJackson2HttpMessageConverter httpMessageConverter = converter.getMessageConverters().stream()
                .filter(mc -> mc.getClass().equals(MappingJackson2HttpMessageConverter.class))
                .map(mc -> (MappingJackson2HttpMessageConverter)mc)
                .findFirst()
                .get();

        httpMessageConverter.getObjectMapper().enable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
    }
}

答案 1 :(得分:1)

易于注释驱动的解决方案。在@Configuration中:

ddict = {}
global analysisdict
analysisdict = {}

for file in os.listdir(path):
    if file.endswith(".txt"):
        name = os.path.splitext(file)[0]
        print(name)
        ddict[name] = import_CHI_file(os.path.join(path, file))
        analysisdict[name] =df_analytics(ddict[name])
return analysisdict

答案 2 :(得分:0)

这种情况正在发生,因为早期版本的spring提供的HttpMessageConverter使用ObjectMapper默认配置。但是较新版本的spring使用Jackson2ObjectMapperBulider,默认情况下DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES属性设置为false。 (参考link)。您可以通过在applicationContext.xml中添加以下内容来解决此问题:

<bean id="jacksonObjectMapper" class="com.fasterxml.jackson.databind.ObjectMapper" />

<mvc:annotation-driven
    content-negotiation-manager="contentNegotiationManager">
    <mvc:message-converters>
        <bean
            class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
            <property name="objectMapper" ref="jacksonObjectMapper" />
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>
<bean id="contentNegotiationManager"
    class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
    <property name="favorPathExtension" value="false" />
    <property name="mediaTypes">
        <value>
            json=application/json
        </value>
    </property>
</bean>

答案 3 :(得分:0)

@Aarya的回答对我来说不起作用,但给了我一个很好的提示来环顾四周。所以这对我有用。鉴于我的春季是4.3.12.RELEASE和杰克逊是2.9.2

<mvc:annotation-driven>
    <mvc:message-converters>
        <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
            <property name="objectMapper">
                    <bean class="com.fasterxml.jackson.databind.ObjectMapper" />
            </property>
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>