Sometimes we use JSON as the standard data Interchange format when interacting with external systems, and for security reasons we increase the checksum of the message, so we need to make sure that the parameters are ordered and not quite as good as the external api, which is not as compatible as the backend and front-end interactions, It is a very high demand for rigor. Jackson's default mechanism is when serializing, the attributes of the father's class, and then the properties of the subclass are in the order defined (fastjson is just the opposite, serializing the subclass and then serializing the parent class). In order to use the JSON tool class, and to achieve an orderly purpose, before specifically studying the Fastjson/jackson of both the serialization characteristics, Fastjson/jackson have set the sequence of sequencing parameters, that is, Jackson @ The index of the jsonproperty annotation and the ordinal of the Fastjson @JSONField annotations. Actually set the test down, feeling that neither is effective or has a bug, in short, the behavior is more bizarre. I remember Jackson had a class-level @jsoninclude annotation that could set all the attributes to serialize, so it was tested specifically to meet the expected requirements, but the jsonignoreproperties was Mistaken. Combined with inheritance [email protected] annotations, We can even achieve a perfect fit for the same properties such as state, some view to be called orderstatus, and others called Shippingstatus goals, while ensuring service/ The mapper layer is completely unrelated to the SCENE. Jackson Jsonignoreproperties/jsonpropertyorder Only reads the annotations of the current class when serializing and deserializing, ignoring the annotations of the parent class, so that we can do 100% precise control of the behavior, and reuse the definition of the parent class as much as Possible.
As follows:
packagetf56.lf.lfoms.model.pub;Importjava.io.Serializable;Importjava.util.List;Importjavax.validation.constraints.NotNull;Importcom.fasterxml.jackson.annotation.JsonFormat;Importcom.fasterxml.jackson.annotation.JsonIgnoreProperties;Importcom.fasterxml.jackson.annotation.JsonInclude;Importcom.fasterxml.jackson.annotation.JsonPropertyOrder;ImportLombok. Getter;ImportLombok. Setter;Importtf56.lf.base.metadata.validate.ValidServices;Importtf56.lf.common.util.JacksonHelper;Importtf56.lf.lfoms.validate.group.Group;/** * @authorAdmin **/@Getter @setter@deprecated@JsonPropertyOrder ({"fromoutpartytype", "receivermobile", "sessionbean"})@JsonIgnoreProperties ({"businesspartid", "omspaymentcollectionlist", "requestorderno"}) public classTmscreaterequestorderiqreqdtoextendsTmscreaterequestordermainreqdtoImplementsserializable{Private Static Final LongSerialversionuid = 8617922710046163090L; //Cost List@ValidServices (services =Group.rquerstorder_create) @NotNull @JsonFormatPrivateList<createomspaymentcollectionreq>omspaymentcollectionlist; //List of goods@ValidServices (services =Group.rquerstorder_create) @NotNullPrivateList<creategoodsdoreq>goodslist; public Static voidmain (string[] Args) {tmscreaterequestorderiqreqdto dto=NewTmscreaterequestorderiqreqdto (); System.out.println (jacksonhelper.tojson (dto)); }}
Set on the Tmscreaterequestordermainreqdto class:
@JsonPropertyOrder ({"sessionbean", "fromoutpartytype", "receivermobile"})
@JsonIgnoreProperties ({"sendermobile"})
The output is as Follows:
{"fromoutpartytype": null, "receivermobile": null, "sessionbean": null, "tfsign": null, "senderpartyid": null, " SenderName ": null," sendermobile ": null," fromdistrict ": null," fromaddress ": null," senderorganization ": null," Tooutpartytype ": null," Receiverpartyid ": null," receiverpartyname ": null," todistrict ": null," toaddress ": null," Receiverorganization ": null," Carrierspartyid ": null," carrierscompany ": null," clientnumber ": null," Transportmethod " : null, "deliverymethod": null, "isneedreceipt": null, "receiptmethod": null, "receiptnum": null, "ordersource": null, " Terminal ": null," Memo ": null," goodslist ": null}
But in general, Jackson should provide jsonproperties annotations, after all, only provide ignore, do not provide positive always sometimes can not directly meet the same, like the black and white list of the same reason.
however, This only solves the Single-item serialization problem, and when deserializing, the same needs to be resolved. If the exposed interface is mapped directly through the JSON requestbody, it is only possible to define a one by one corresponding interface proxy class to be Mapped.
Note by Jackson Jsonpropertyorder and @jsonignoreproperties