@JSON View

What is @JsonView?

@JsonView is an annotation from the Jackson library that allows us to define different views of the same object. This helps in customizing the serialization of JSON responses without modifying the core model.

Why Use @JsonView?

Role-based JSON Views – Show different data for Admin and User roles.

API Versioning – Expose different fields for v1 and v2 of an API.

Efficient Serialization – Reduce unnecessary data transfer over the network.

1. Dependency for @JsonView in Spring Boot

Maven Dependency

				
					<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.15.0</version>
</dependency>
				
			

Gradle Dependency

				
					implementation 'com.fasterxml.jackson.core:jackson-databind:2.15.0'
				
			

2. Enable Required Key in application.properties

               Jackson is enabled by default in Spring Boot. However, if needed, ensure that serialization settings are properly configured.

application.properties

				
					spring.jackson.mapper.default-view-inclusion=true
				
			

       This ensures that fields marked with @JsonView are properly included when serializing.

3. Example for @JsonView

       @JsonView is used to control which fields should be included in JSON responses based on different views. Here’s a complete example:

Step 1: Create DTO with Two Interfaces

				
					import com.fasterxml.jackson.annotation.JsonView;
@Data
public class UserDTO {

    public interface BasicView {}
    public interface DetailedView extends BasicView {}

    @JsonView(BasicView.class)
    private Long id;

    @JsonView(BasicView.class)
    private String name;

    @JsonView(DetailedView.class)
    private String email;

    @JsonView(DetailedView.class)
    private String phoneNumber;

}

				
			

Step 2: Create Controller

				
					import com.fasterxml.jackson.annotation.JsonView;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/users")
public class UserController {

    @GetMapping("/basic")
    @JsonView(UserDTO.BasicView.class)
    public UserDTO getBasicUser() {
        return new UserDTO(1L, "John Doe", "john.doe@example.com", "123-456-7890");
    }

    @GetMapping("/detailed")
    @JsonView(UserDTO.DetailedView.class)
    public UserDTO getDetailedUser() {
        return new UserDTO(1L, "John Doe", "john.doe@example.com", "123-456-7890");
    }
}
				
			

4. Testing the API

Basic View (/users/basic)

				
					{
  "id": 1,
  "name": "John Doe"
}
				
			

Detailed View (/users/detailed)

				
					{
  "id": 1,
  "name": "John Doe",
  "email": "john.doe@example.com",
  "phoneNumber": "123-456-7890"
}
				
			

         @JsonView is provided by Jackson, so you need to include the Jackson dependency in your pom.xml if you’re using Maven:

💬 Have you used @JsonView in your projects? Share your experience in the comments! 📢 Found this helpful? Share it with your fellow developers!

Share the Post: