Business Scenario: The user sends the order request in the format XML format, the server receives the data to complete the order and returns the result to the customer.
Request Format:
<Request> <Head> < Sign></ Sign> < Time></ Time> <msg></msg> </Head> <Body> <productId></productId> <Count></Count> < Price></ Price> <User> <userId></userId> <name></name> <Mobil></Mobil> <Email></Email> </User> <Extendinfos> <Extendinfo> < City></ City> <Province></Province> <District></District> <Address></Address> </Extendinfo> </Extendinfos> </Body></Request>
This request message is in XML format, we can use Javax.xml.bind.annotation to format the data in the background.
We can do the data corresponding to the DTO is annotated with @xmlelement annotation corresponding node
Like what:
Importjava.io.Serializable;ImportJavax.xml.bind.annotation.XmlAccessType;ImportJavax.xml.bind.annotation.XmlAccessorType;Importjavax.xml.bind.annotation.XmlElement;Importjavax.xml.bind.annotation.XmlRootElement;ImportOrg.apache.commons.lang.builder.ToStringBuilder;ImportOrg.apache.commons.lang.builder.ToStringStyle;ImportCom.juqitech.adapter.dto.ChannelDTO; @XmlRootElement (name= "Request") @XmlAccessorType (xmlaccesstype.none) Public classRequestdtoImplementsSerializable {Private Static Final LongSerialversionuid =-1; @XmlElementPrivaterequestheaderdto Head; @XmlElementPrivaterequestbodydto body; Publicrequestheaderdto GetHead () {returnHead; } Public voidSethead (requestheaderdto head) { This. Head =Head; } Publicrequestbodydto GetBody () {returnbody; } Public voidSetbody (requestbodydto body) { This. BODY =body; }}
@XmlRootElement (name = "Request") can be defined on the class, representing the root node
@XmlAccessorType (Xmlaccesstype.field)
Represents a property or element that uses private non-static fields in this class as XML serialization. You can also choose a property, which is to serialize a property or element using the Set/get method.
The key is to have a Extendinfos array in the request, generally we will use list to represent, so in Requestbodydto can write:
Importjava.io.Serializable;ImportJavax.xml.bind.annotation.XmlAccessType;ImportJavax.xml.bind.annotation.XmlAccessorType;Importjavax.xml.bind.annotation.XmlElement;Importjavax.xml.bind.annotation.XmlRootElement;ImportOrg.apache.commons.lang.builder.ToStringBuilder;ImportOrg.apache.commons.lang.builder.ToStringStyle;ImportCom.juqitech.adapter.dto.ChannelDTO; @XmlRootElement (name= "Body") @XmlAccessorType (xmlaccesstype.none) Public classRequestbodydtoImplementsSerializable {Private Static Final LongSerialversionuid =-1; //.... The preceding attribute is omitted@XmlElementWrapper (Name= "Extendinfos") @XmlElement (name= "Extendinfo") PrivateList<extendinfo>Extendinfos; //.. the other omitted}
@XmlRootElement (Name= "Extendinfo") @XmlAccessorType (xmlaccesstype.none) Public classextendinfo{@XmlElementPrivateString City; @XmlElementPrivateString Province; @XmlElementPrivateString District; //omitted}
@XmlElementWrapper This comment is used outside the package layer to process the list collection
about how Java backend receives XML-formatted data