Jackson application three Object Property Control

Source: Internet
Author: User

Generally, the full data binding technology introduced in the second article quickly enjoys Jackson's convenience and convenience. However, we soon encountered some small problems,

How to ignore certain attributes and prevent them from being involved in serialization. (Please do not get it wrong. This is not about the Java serializable mechanism)

It's quite simple, just an annotation. Modify the response. Java file and add @ jsonignore to the Message attribute.

import com.fasterxml.jackson.annotation.JsonIgnore;import lombok.Getter;import lombok.Setter;/** * * @author chenshu */public class Response {        @Getter @Setter    private String status;        @JsonIgnore    @Getter @Setter    private String message;}

The app. Java code is as follows:

/** * Hello world! * */public class App {    public static void main( String[] args ) throws IOException {                        ObjectMapper mapper = new ObjectMapper(); // create once, reuse        String jsonSource = "{\"message\":\"Login succeeded!\",\"status\":0}";        Response response = mapper.readValue(jsonSource, Response.class);                String result = mapper.writeValueAsString(response);    }}

The result is that the message field of the response object is null. After being serialized into a string result, only the status is included.

To modify the attribute name (the exact description is the name of the getxx method of the attribute), use the following annotation:

@JsonProperty("newName")

Reference: https://github.com/FasterXML/jackson-databind

Soon there was another demand. If I need to serialize several attributes of a class to JSON objects in one case, I need to serialize several other attributes to JSON objects in another case. The above method won't work.

For details, refer to the following document JSON Filter

Http://wiki.fasterxml.com/JacksonFeatureJsonFilter

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.