How WebService encapsulates XML requests and the XML returned by the parsing interface

< span class="article-type type-1 float-left">Original

How does WebService encapsulate XML requests and parse the XML returned by the interface

< div class="article-info-box">

Top August 16, 2019 15:00:47 Boy Rafting Reading 28 Tags: XML parsing WebService third-party API

Personal classification: JavaWeb

Copyright statement: This article is a blogger’s original article, following CC 4.0 by- sa Copyright agreement, please attach a link to the original source and this statement for reprinting.

Link to this article: https://blog.csdn.net/qq_24818689/article/details/99677155

1. Encapsulate XML message object

The blogger is talking to a third party When interface, it is often necessary to encapsulate XML to request third-party data, which is often used in Web development, so I also plan to write an article to record the process of thinking and seeking answers.

1-1 Some basic knowledge of XML

Generally, when referring to some API documents, JAVA development generally encapsulates data according to specific API requirements. Here, I An example will be used to illustrate the scenarios that have been applied. When encapsulating XML objects, we must first understand the trial method of encapsulating XML objects, which is generally implemented in the form of Class annotations. Such as @XmlType, @XmlAccessorType, @XmlRootElement, @XmlElement, etc.

@XmlType(propOrder ={ “Header”, “MessageType”, “Message” }) // Specify the sequence of xml node order

@XmlAccessorType(value = XmlAccessType.FIELD ) // Change the access type to a field

@XmlRootElement(name = “AmazonEnvelope”)//Encapsulate the root node of the XML object

1-2 Encapsulate XML for some specific API requests parameter. Here is an example of docking with some Amazon interfaces

The following is an example for me to add an interface that requires encapsulation of XML for parameters:

  
  1. /*
  2. < div class="hljs-ln-line hljs-ln-n" data-line-number="2">
* xml version="1.0" encoding="UTF-8"?>
  • * <AmazonEnvelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="amzn-envelope.xsd">
  • * <Header>
  • * <DocumentVersion>1.02 < span class="hljs-tag">DocumentVersion>
  • * <MerchantIdentifier>A23G8Q8ZIKBK8C MerchantIdentifier>
  • * Header< /span>>
  • * <MessageType>ProcessingReport MessageType>
  • < div class="hljs-ln-line hljs-ln-n" data-line-number="9">
  • * <Message>
  • * <MessageID>1 MessageID>
  • * <ProcessingReport>
  • * <DocumentTransactionID>57320017876 DocumentTransactionID>
  • * <StatusCode>Complete StatusCode>
  • * <ProcessingSummary> < /div>
  • * <MessagesProcessed>15 MessagesProcessed span>>
  • * <MessagesSuccessful>13 MessagesSuccessful span>>
  • * <MessagesWithError>2 MessagesWithError>
  • * <MessagesWithWarning>0 MessagesWithWarning>
  • * ProcessingSummary>
  • * <Result>
  • * <MessageID>3 MessageID>
  • * <ResultCode>Error ResultCode>
  • * <ResultMessageCode>25 ResultMessageCode>
  • * <ResultDescription>We are unable to process the XML feed because one or more items are invalid. Please re-submit the feed. ResultDescription> < /div>
  • * Result>
    < /li>
  • * <Result< /span>>
  • * <MessageID>4 MessageID>
  • * <ResultCode>Error ResultCode>
  • * <ResultMessageCode>25 ResultMessageCode>
  • * <ResultDescription>We are unable to process the XML feed because one or more items are invalid. Please re -submit the feed. ResultDescription>
  • < li>
    * Result>
  • * ProcessingReport>
  • * Message>
  • * AmazonEnvelope>
  • */
  • If Seeing this XML format, how to encapsulate the request object?

    If we have understood the language XML, we know that XML can be understood as a tree composed of parent-child root nodes. In fact, when Spring internally parses XML, it also parses according to this feature. Because our most primitive MVC requires a lot of configuration XML to inject beans. And configure things and so on. Through analysis, we can find that the external root node is AmazonEnvelope, the child nodes Header, MessageType, and Message, and then there are child nodes MessageID and ProcessingReport under the Message node. By analogy, you can construct a large AmazonEnvelope object, and then use it as the root node to build child node objects. Here are two examples as follows:

      
    1. package com.aukey.supply.chain.domain.test;
    2. < li>
    3. < div class=" hljs-ln-line hljs-ln-n" data-line-number="3">
    import javax.xml.bind. annotation.XmlAccessType;
  • < /div>
    import javax.xml.bind. annotation.XmlAccessorType;
  • import javax.xml.bind. annotation.XmlElement;
  • import javax.xml.bind. annotation.XmlRootElement;
  • im port javax.xml.bind. annotation.XmlType;
  • < span class="hljs-meta">@XmlType(propOrder =
  • < /div>
    { "Header", "MessageType", "Message" })< /span> // Specify the sequence of xml node order
  • < div class="hljs-ln-line"> @XmlAccessorType(value = XmlAccessType.FIELD) // Change the access type Field
  • @XmlRootElement(name = "AmazonEnvelope")
  • public class < span class="hljs-title">AmazonEnvelope {
  • @XmlElement
  • private Header Header; //Construct the head
  • @XmlElement
  • private String MessageType;
  • @XmlElement
  • private Message Message;
  • public Header getHeader() {
  • return Header;
  • }
  • public void setHeader(Header header) {
  • Header = header;
  • }
  • public String getMessageType() {
  • return MessageType;
  • }
  • public void setMessageType(String messageType) {
  • MessageType = messageType;
  • }
  • public Message getMessage() {
  • return Message;
  • }
  • public void setMessage(Message message) {
  • Message = message;
  • }
  • < div class="hljs-ln-code">
    }