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