Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to ignore some attributes when converting Java Pojo to Jsoy

2025-03-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the knowledge of "how to ignore some attributes when converting Java Pojo to Jsoy". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Preface

In Java development, sometimes some sensitive information needs to be blocked and cannot be known by the client that consumes the data. Usually we set it to null or the empty character "", but there are other ways, if you use Jackson. Next I'm going to show you what you can do with a real scene.

2. How Jackson ignores fields

JSON serialization is used here as an example. If we need to return user information in our business, the existing POJO looks like this:

Import lombok.Data; / * * @ author felord.cn * / @ Data public class UserInfo {/ * userid * / private String userId; / * * user name * / private String username; / * keychain * / private String secret; / * address information * / private String address;}

Business scenario: a third party obtains the user's information through the user's userId, but the keychain secret obviously cannot be made known to the third party, and the easiest way to think of is to set the secret field to null or "". If the business needs to provide user information in bulk, that is, List, we can't go through it every time. Spring Boot's built-in Jackson can easily help us deal with this problem.

Use @ JsonIgnore annotations

Jackson provides an @ JsonIgnore annotation that can be masked by marking it on the field that needs to be ignored or on the corresponding getter method or setter method. It is marked like this:

@ JsonIgnore private String secret; / / corresponding json sample {"userId": "100000", "username": "felord.cn", "address": "cn"}

Whether serialization (converting POJO to JSON) or deserialization (converting JSON to POJO), secret is ignored.

Use @ JsonIgnoreProperties annotations

This annotation is a little more powerful than @ JsonIgnore. Usually it should be marked on top of POJO, which has more capabilities:

Ignore multiple fields and configure the value property.

Ignore unknown attributes. Configure ignoreUnknown to true, which is not ignored by default.

Allow ignore fields to be serialized, configure allowGetters to true, and will not be ignored when serialized.

Allow ignored fields to be deserialized, configure allowSetters to true, and will not be ignored when deserialized.

For example, if we want to ignore secret and address in UserInfo, we can configure it like this:

@ JsonIgnoreProperties ({"secret", "address"})

Use @ JsonProperty annotations

Jackson version is required to be at least 2.6

The appearance rate of this annotation is still very high, and it is usually used to alias the JSON field or set the default value. For example, if the userId in UserInfo wants to correspond to the user_id in JSON, we can:

@ JsonProperty (value = "user_id") private String userId

After version 2.6, this annotation can also ignore fields. It has an access attribute that specifies access during serialization ("read") and deserialization ("write") (in this case, from the perspective of the property). It is defined by the enumeration Access:

Public enum Access {/ * whether serialization or deserialization is automatically processed according to the configuration, default value. * / AUTO, / * means that the property can only be read during serialization (the value accessed through the "getter" method, or read from the field) and cannot be written (set) during deserialization *. In other words, this will reflect a "read-only POJO" that contains values that can be read but not written. * / READ_ONLY, / * means that the property can only be written (set) as part of deserialization (using the "setter" method, or assigned to Field, or passed as a * Creator parameter) and not read (obtained) for serialization, that is, the value of the property is not included in the serialization. * / WRITE_ONLY, / * is readable and writable, and the combined effect of READ_ONLY and WRITE_ONLY. * / READ_WRITE;}

We can see from this note that if you want to ignore the secret field when converting POJO to JSON, you can write:

@ JsonProperty (access = JsonProperty.Access.WRITE_ONLY) private String secret

Use @ JsonIgnoreType annotations

This annotation is used to ignore types directly. If the UserInfo above is a property of another POJO and we don't want it to be serialized and deserialized, then we can:

@ JsonIgnoreType public class UserInfo {/ / omit} "how to ignore some attributes when converting Java Pojo to Jsoy" is introduced here. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report