Managing Nested Object Serialization with @JsonView in Spring Boot
Serialization and deserialization play a vital role in building modern web applications. When working with nested objects, handling serialization while maintaining flexibility in exposing data becomes a challenge. How can you customize the way yo...

I am Tuanh.net. As of 2024, I have accumulated 8 years of experience in backend programming. I am delighted to connect and share my knowledge with everyone.
1. What is @JsonView?
1.1 How @JsonView Works
public class Views {
public interface Public {}
public interface Internal extends Public {}
}
1.2 Why is @JsonView Useful?
2. Implementing @JsonView in Nested Objects
2.1 Defining the Models
import com.fasterxml.jackson.annotation.JsonView;
public class User {
@JsonView(Views.Public.class)
private String name;
@JsonView(Views.Public.class)
private String email;
@JsonView(Views.Internal.class)
private String ssn;
@JsonView(Views.Public.class)
private Address address;
@JsonView(Views.Internal.class)
private List<Order> orders;
// Getters and setters
}
import com.fasterxml.jackson.annotation.JsonView;
public class Address {
@JsonView(Views.Public.class)
private String street;
@JsonView(Views.Public.class)
private String city;
@JsonView(Views.Internal.class)
private String zipCode;
// Getters and setters
}
import com.fasterxml.jackson.annotation.JsonView;
public class Order {
@JsonView(Views.Public.class)
private String orderId;
@JsonView(Views.Internal.class)
private Double amount;
// Getters and setters
}
2.2 Configuring @JsonView in Controllers
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import com.fasterxml.jackson.annotation.JsonView;
@RestController
public class UserController {
@GetMapping("/user/public")
@JsonView(Views.Public.class)
public User getUserPublicView() {
return getSampleUser();
}
@GetMapping("/user/internal")
@JsonView(Views.Internal.class)
public User getUserInternalView() {
return getSampleUser();
}
private User getSampleUser() {
Address address = new Address();
address.setStreet("123 Main St");
address.setCity("New York");
address.setZipCode("10001");
Order order1 = new Order();
order1.setOrderId("ORD123");
order1.setAmount(150.75);
User user = new User();
user.setName("John Doe");
user.setEmail("john.doe@example.com");
user.setSsn("123-45-6789");
user.setAddress(address);
user.setOrders(List.of(order1));
return user;
}
}
2.3 Testing the Output
{
"name": "John Doe",
"email": "john.doe@example.com",
"address": {
"street": "123 Main St",
"city": "New York"
}
}
{
"name": "John Doe",
"email": "john.doe@example.com",
"ssn": "123-45-6789",
"address": {
"street": "123 Main St",
"city": "New York",
"zipCode": "10001"
},
"orders": [
{
"orderId": "ORD123",
"amount": 150.75
}
]
}
3. Benefits and Best Practices
4. Beyond the Basics: Advanced Scenarios
@GetMapping("/user/custom")
public MappingJacksonValue getUserCustomView(@RequestParam String view) {
User user = getSampleUser();
MappingJacksonValue jacksonValue = new MappingJacksonValue(user);
if ("public".equalsIgnoreCase(view)) {
jacksonValue.setSerializationView(Views.Public.class);
} else {
jacksonValue.setSerializationView(Views.Internal.class);
}
return jacksonValue;
}
5. Conclusion
Read more at : Managing Nested Object Serialization with @JsonView in Spring Boot





