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

Mas*_*bha 6 spring spring-mvc jackson-dataformat-xml jackson2

@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>
Run Code Online (Sandbox Code Playgroud)

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

public class Student{ 
    private String id; 
    private String name; 
}
Run Code Online (Sandbox Code Playgroud)

如果发送有效的相应 json 请求,它应该是这样的

{ 
   "id": "123", 
   "name": "test" 
}
Run Code Online (Sandbox Code Playgroud)

但即使我发送带有无效字段的 json 请求,如下所示,它仍然接受。

{ 
    "id": "123", 
    "name": "test", 
    "anyinvalidkey": "test" 
}
Run Code Online (Sandbox Code Playgroud)

但它应该给出错误的请求作为响应

小智 6

基于 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);
    }
}
Run Code Online (Sandbox Code Playgroud)


Aar*_*rya 0

发生这种情况是因为HttpMessageConverter早期版本的 spring 提供的是ObjectMapper默认配置。但是较新版本的 spring 使用的Jackson2ObjectMapperBulider属性DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES默认设置为 false。(参考链接)。您可以通过在 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>
Run Code Online (Sandbox Code Playgroud)