@JsonIgnoreProperties (ignoreUnknown = false) does not work in Spring 4.2.0 and in the upper version

@JsonIgnoreProperties (ignoreUnknown = false) does not work with spring 4.2.0 and the upper version of spring. But it works with 4.0.4 and 4.0.1. I use spring 4.2.8 and Jackson dependencies are used

<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>

If I send a json request with invalid fields, it accepts as a valid request. But he has to give a bad request as an answer. For example: if I have a class

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

If you send a valid matching json request, it should look like

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

But even if I send a json request with invalid fields as shown below, it still accepts.

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

But he should give a bad request as an answer

+1
4

, 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

, 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>
0

@ , , . . , Spring 4.3.12. REELASE jackson - 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>
0
source

A simple solution based on annotations. In @Configuration:

@Bean
@Primary
public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) {
    ObjectMapper objectMapper = builder.createXmlMapper(false).build();
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true);
    return objectMapper;
}
0
source

Source: https://habr.com/ru/post/1677501/


All Articles