Friday, November 10, 2017

Spring framework - Jackson UnrecognizedPropertyException not thrown

Jackson UnrecognizedPropertyException not thrown

This post is to show how to force newer version of Spring (4.1.1.RELEASE and later) to return error when a JSON with unrecognized fields is deserialized.

Setup

Maven
<dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-core</artifactId>
 <version>4.3.12.RELEASE</version>
</dependency>
<dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-webmvc</artifactId>
 <version>4.3.12.RELEASE</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.9.2</version>
</dependency>

Development

User.java

package com.example.model;

public class User {
    private String id;
    private String name;

    // constructor, setters, getters
}

JSON

{
  “id”: “28b60d1e-c650-11e7-abc4-cec278b6b50a”,
  “name”: “John Smith”,
  “nickname”: “Johnny”
}

Controller.java

package com.example.controller;

@RestController
public class Controller {

    private UserService userService;

    @PutMapping(“/user”)
    public void updateUserInfo(@RequestBody User user) {
        userService.updateUser(user);
    }
}
I expected to see UnrecognizedPropertyException or 400 Bad Request when the JSON was sent in, but it did not happen. Adding the annotation @JsonIgnoreProperties(ignoreUnknown=false) to the POJO had no effect, either. It turns out Spring Framework has changed its default configuration. So in the Spring configuration xml, I have to override its behavior

context-config.xml

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