Suppose I have the following classes:
public class Employee {
private Department department;
}
public class Department {
private Address address;
}
public class Address {
private Location location;
}
public class Location {
private String streetName;
}
Now, I want to load an object Employee
and serialize it with ObjectMapper
:
public void serializeEmployee() {
Employee employee = entityManager.load(Employee.class, 1);
ObjectMapper mapper = new ObjectMapper();
System.out.println(mapper.writeValueAsString(student));
}
when I run the code above, I see the json line as follows:
{
"department" : {
"address" : {
"location" : {
"streetName" : {}
}
}
}
}
but I want to set the serialization depth to one level, I mean, when the code starts, I want to see the result as follows:
{
"department" : {
}
}
Note
I do not want to use jackson annotations, I want to configure the configuration when using the object mapper
. for exmaple with mapper.setConfig
or call mapper.disable
.
source
share