Spring Boot: Dynamically Ignore Fields While Serializing Java Objects to JSON
Learn how to ignore fields at runtime.
Join the DZone community and get the full member experience.
Join For FreeLet's say that we have a UserController class with two GET endpoints:
- /users/{id} endpoint, which returns a User object for a given id
- /users endpoint, which returns List<User>
xxxxxxxxxx
public class User {
private Integer id;
private String name;
private Date dateOfBirth;
private String city;
// constructors, getters & setters are ignored
}
public class UserController {
UserService userService;
value = "/user/{id}", method= RequestMethod.GET) (
User getUser( ("id") String id){
return userService.getUser(id);
}
value = "/users", method= RequestMethod.GET) (
List<User> getAllUsers(){
return userService.getAllUsers();
}
}
Our client wants the id and name of all users in a JSON List when the /users endpoint is called. For the /users/{id} endpoint, they want the name, dateOfBirth, and city for the given user id.
If we use the User POJO class for both the endpoints, we will get the id, name, dateOfBirth, and city all the time. So we have to figure out a way to ignore fields at the time of serialization.
The Jackson library provides two annotations: @JsonIgnore and @JsonIgnoreProperties to ignore fields while serializing Java Objects to JSON. But both these annotations need the fields at compile time itself.
But in our case, the fields to be ignored have to be decided at the runtime, based on the endpoint called.
However, we can use MappingJacksonValue to ignore fields dynamically.
Here is a sample implementation:
x
public class User {
private Integer id;
private String name;
private Date dateOfBirth;
private String city;
// constructors, getters & setters are ignored
}
public class UserController {
UserService userService;
value = "/user/{id}", method= RequestMethod.GET) (
MappingJacksonValue getUser( ("id") String id){
SimpleBeanPropertyFilter simpleBeanPropertyFilter =
SimpleBeanPropertyFilter.serializeAllExcept("id");
FilterProvider filterProvider = new SimpleFilterProvider()
.addFilter("userFilter", simpleBeanPropertyFilter);
User user = userService.getUser(id);
MappingJacksonValue mappingJacksonValue = new MappingJacksonValue(user);
mappingJacksonValue.setFilters(filterProvider);
return mappingJacksonValue;
}
value = "/users", method= RequestMethod.GET) (
MappingJacksonValue getAllUsers(){
SimpleBeanPropertyFilter simpleBeanPropertyFilter =
SimpleBeanPropertyFilter.serializeAllExcept("dateOfBirth", "city");
FilterProvider filterProvider = new SimpleFilterProvider()
.addFilter("userFilter", simpleBeanPropertyFilter);
List<User> userList = userService.getAllUsers();
MappingJacksonValue mappingJacksonValue = new MappingJacksonValue(userList);
mappingJacksonValue.setFilters(filterProvider);
return mappingJacksonValue;
}
}
Explanation:
- Add the JsonFilter annotation on the POJO class
@JsonFilter("userFilter")
- Pass the name of the fields to be ignored to the serializeAllExcept method from the SimpleBeanPropertyFilter class
SimpleBeanPropertyFilter.serializeAllExcept("dateOfBirth", "city") - will ignore dateOfBirth & city fields
- Pass the JsonFilter name and SimpleBeanPropertyFilter object to the addFilter method
FilterProvider filterProvider = new SimpleFilterProvider().addFilter("userFilter", simpleBeanPropertyFilter);
- Pass the instance of the POJO and filter class to the MappingJacksonValue class
MappingJacksonValue mappingJacksonValue = new MappingJacksonValue(userList);
mappingJacksonValue.setFilters(filterProvider);
- Make sure MappingJacksonValue is the return type of the method
Now Spring will take care of ignoring the fields mentioned in SimpleBeanPropertyFilter on serialization.
That's all folks!
- @IamVickyAV
Published at DZone with permission of Vicky AV. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments