about how Java backend receives XML-formatted data

Source: Internet
Author: User

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

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.