If the jackson object to json is NULL or empty, serialization is not performed.

Source: Internet
Author: User

If the jackson object to json is NULL or empty, serialization is not performed.

1. Entity

@ JsonInclude (Include. NON_NULL)

// Place the tag on the property. If the property is NULL, it is not involved in serialization.
// If it is placed on the top of the class, it takes effect for all the attributes of the class.
// Include. Include. ALWAYS default
// Include. NON_DEFAULT attribute is default value not serialized
// Include. NON_EMPTY property is NULL ("") or NULL is not serialized
// Include. NON_NULL attribute is NULL and is not serialized


2. Code
ObjectMapper mapper = new ObjectMapper ();

Mapper. setserializationsion Sion (Include. NON_NULL );

// Set the mapper object using this method. All serialized objects are serialized according to the modified rules.
// Include. Include. ALWAYS default
// Include. NON_DEFAULT attribute is default value not serialized
// Include. NON_EMPTY property is NULL ("") or NULL is not serialized
// Include. NON_NULL attribute is NULL and is not serialized

User user = new User (1, "", null );
String outJson = mapper. writeValueAsString (user );
System. out. println (outJson );



Jackson object to json attributes directly does not participate in serialization

---------------------------------------------
@ JsonIgnore

Private int id;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.