Preface
Recently, during the learning process, I discovered that the @JsonView of the Jackson library can also change the output structure of JSON and can control it more finely. I usually define some DTOs to encapsulate response data. Using this method, you can achieve it in a more elegant and efficient way without creating DTOs. 🌈
1. How to use the @JsonView annotation?
We can create multipleview, each view represents a specific set of fields. Then, on the controller's method, we can apply these views to specify which fields should be used to serialize the returned entity object. This flexibility makes it easy for us to deal with a variety of complex business needs.
2. Application scenarios
- Control of data exposure level:
Suppose you have a user entity (User) that contains some basic information (such as name, age) and some sensitive information (such as password, email, ID card). When you need to provide an API to get basic information about users, you may not want to expose sensitive information. By using@JsonView
, you can define two views: one with basic information and the other with all information. Then, on the corresponding API method, you can use the corresponding view to control the output data.
Customize data according to user role:
In some cases, different user roles may need to see different levels of data. For example, an administrator may need to see all the information of the user, while an average user can only see basic information. By using@JsonView
and Spring Security, you can dynamically decide which data should be exposed based on the user's role.
API version control:
As your API evolves over time and introduces new features, there may be multiple versions of the API. In some cases, older versions of the API may not need to include newly introduced fields. pass@JsonView
, you can define a view for each API version and switch to the corresponding view if needed.
Data aggregation and splitting:
In some cases, you may need to aggregate data from multiple entities into a JSON response, or split the data from a large entity into multiple small entities. By using@JsonView
, you can more easily implement this kind of data aggregation and splitting.
3. Practical cases
Define view rules:
public interface UserView { // Show basic information interface Basic {} // No password interface WithoutPassword extends Basic {} // Show all information interface Full extends Basic {} }
Define a User, add @JSsonView annotation on the property, and specify the view
@Data @AllArgsConstructor public class User { @JsonView() private Long id; @JsonView() private String username; @JsonView() private String password; @JsonView() private String nickName; @JsonView() private String email; @JsonView({,}) private String phone; }
There are two ways to specify a view on the controller method: annotation method and programming method.
Annotation method
@RestController public class testController { @GetMapping("/user/basic") @JsonView() public User userBasic(){ User user = new User(1L,"admin","123456","Xiaojie is not bald","123456@","188xxxxxxxx"); return user; } @GetMapping("/user/noPassword") @JsonView() public User userNoPassword(){ User user = new User(1L,"admin","123456","Xiaojie is not bald","123456@","188xxxxxxxx"); return user; } @GetMapping("/user/full") @JsonView() public User userFull(){ User user = new User(1L,"admin","123456","Xiaojie is not bald","123456@","188xxxxxxxx"); return user; } }
Notice:@JsonViewAllow an array of view classes, but you can only specify one for each controller method. If multiple views need to be activated, you can use the composite interface.
Test the above three interfaces:
// user/basic { "id": 1, "username": "admin", "nickName": "Xiaojie is not bald", "email": "123456@", "phone": "188xxxxxxxx" } // user/noPassword { "id": 1, "username": "admin", "nickName": "Xiaojie is not bald", "email": "123456@", "phone": "188xxxxxxxx" } // user/full { "id": 1, "username": "admin", "password": "123456", "nickName": "Xiaojie is not bald", "email": "123456@", "phone": "188xxxxxxxx" }
Programming method
@RestController public class testController { @GetMapping("/user/basic") //@JsonView() public MappingJacksonValue userBasic(){ User user = new User(1L,"admin","123456","Xiaojie is not bald","123456@","188xxxxxxxx"); MappingJacksonValue mappingJacksonValue = new MappingJacksonValue(user); (); return mappingJacksonValue; } @GetMapping("/user/noPassword") //@JsonView() public MappingJacksonValue userNoPassword(){ User user = new User(1L,"admin","123456","Xiaojie is not bald","123456@","188xxxxxxxx"); MappingJacksonValue mappingJacksonValue = new MappingJacksonValue(user); (); return mappingJacksonValue; } @GetMapping("/user/full") //@JsonView() public MappingJacksonValue userFull(){ User user = new User(1L,"admin","123456","Xiaojie is not bald","123456@","188xxxxxxxx"); MappingJacksonValue mappingJacksonValue = new MappingJacksonValue(user); (); return mappingJacksonValue; } } //The test results are consistent with the above
Note: The interface return value must beMappingJacksonValue。
Summarize
@JsonViewProvides a flexible and powerful way to control serialized JSON data. By defining views and applying them to properties and controller methods, you can expose different levels of data as needed to customize data for APIs.
@JsonViewIt can also be used in combination with other annotations to enable more detailed control, such as:@JsonInclude、@JsonIgnoreetc.。
This is the end of this article about the implementation of SpringBoot customized JSON response data. For more related SpringBoot customized JSON content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!