Spring Boot JSON — Jackson
JSON (JavaScript Object Notation) is an open standard file and data interchange format that uses human-readable text to store and transmit data objects. Spring Boot comes with three integrations of JSON mapping libraries including Jackson, Gson, and JSON-B. But in these 3 options, Jackson is the default and preferred library. You can use the following property to change the default mapping library if needed.
spring.mvc.converters.preferred-json-mapper=jsonb
Jackson
Jackson is a suite of data-processing tools for Java (and the JVM platform), including the flagship streaming JSON parser/generator library, matching data-binding library (POJOs to and from JSON), and additional data format modules to process data encoded in Avro, BSON, CBOR, CSV, Smile, (Java) Properties, Protobuf, TOML, XML or YAML.
In order to use it in the Spring Boot, auto-configuration for Jackson is provided and Jackson is part of spring-boot-starter-json
. but if you already have spring-boot-starter-web
you will not need it because it is already loaded as part of the web starter.
Jackson ObjectMapper
Using object mapper you can map the JSON to the object or vice versa. this can be done in various ways and from different sources. Here is a simple mapping from JSON to Java Object.
JSON String to Java Object :
ObjectMapper om = new ObjectMapper();
String json = "{ \"name\" : \"John\" , \"age\" : 10 }";
try {
Student student = om.readValue(json, Student.class);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
Java Object To JSON file:
ObjectMapper om = new ObjectMapper();
Student student= new Student("John", 19);
try {
objectMapper.writeValue(new File("target/student.json"), student);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
All of the mappings between Java objects and JSON in incoming and outgoing requests and responses in the controller are handled with Jackson. Having Jackson on the classpath, you already get the default converter(s) provided by Jackson2ObjectMapperBuilder
, an instance of which is auto-configured for you.
Customize the Jackson ObjectMapper
There are multiple approaches for customizing Jackson ObjectMapper. You can choose the proper method based on the simplicity or flexibility that you need.
Application.properties: Using application properties is the simplest way to customize the Jackson ObjectMapper. The downside of this approach is that we can’t customize advanced options.
# spring.jackson.<category_name>.<feature_name>=<value>
spring.jackson.default-property-inclusion=non_null
spring.jackson.serialization.write-dates-as-timestamps=false
...
Jackson2ObjectMapperBuilderCustomizer: Another Option is to use Jackson2ObjectMapperBuilderCustomizer
customizing. This function interface provides the necessary tools to change the required setting of the JSON mapper.
@Bean
public Jackson2ObjectMapperBuilderCustomizer jc() {
return builder ->
builder
.serializationInclusion(JsonInclude.Include.NON_NULL);
}
Jackson2ObjectMapperBuilder: It is also possible to directly create the bean for the mapper builder instead of using a customizer.
@Bean
public Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder() {
return new Jackson2ObjectMapperBuilder()
.serializationInclusion(JsonInclude.Include.NON_NULL);
}
Annotation: At the class level you also have the option to use the annotation to change some of the mapping configurations for that class. A full list of annotations is available on GitHub.
@JsonInclude(Include.NON_NULL)
public class Student {
private String name;
private Long age;
}
ObjectMapper: The ObjectMapper
itself gives some options to change as it is being called directly in any use case.
ObjectMapper mapper = new ObjectMapper(); mapper.setSerializationInclusion(Include.NON_NULL);
Hope this article helped you and please support me with your applauding for the story. if you don’t know how it is just like this:
Or buy me a coffee here!