简体   繁体   English

解析 SOAP 响应 JAVA object

[英]Parsing SOAP response to JAVA object

I am trying to parsing a Soap response string to a JAVA object to get those parameters.我正在尝试将 Soap 响应字符串解析为 JAVA object 以获取这些参数。

Here's the response string:这是响应字符串:

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns0:submitCdus1Response xmlns:ns0="http://example.com/">
<MessageId>D421425215</MessageId>
<NoOfDay>14</NoOfDay>
<Status>Y</Status>
<LastControlPoint>SBC</LastControlPoint>
<LastEntryDate>20210415</LastEntryDate>
<ReplyDateTime>20210427114126848</ReplyDateTime>
<TypeOfTravel>A</TypeOfTravel>
</ns0:submitCdus1Response>
</S:Body>
</S:Envelope>

My Object class:我的 Object class:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name="submitCdus1Response", namespace="http://example.com/" )
public class TravelHistoryResponseDTO implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    @XmlAttribute(name = "MessageId")
    private String MessageId;
    @XmlAttribute(name = "NoOfDay")
    private Integer NoOfDay;
    @XmlAttribute(name = "Status")
    private String Status; //Y , N ,
    @XmlAttribute(name = "LastControlPoint")
    private String LastControlPoint;
    @XmlAttribute(name = "TypeOfTravel")
    private String TypeOfTravel;// A
    @XmlAttribute(name = "LastEntryDate")
    private Date LastEntryDate;
    @XmlAttribute(name = "ReplyDateTime")
    private Date ReplyDateTime;
      
    public String getMessageId() {
        return MessageId;
    }
    public void setMessageId(String messageId) {
        MessageId = messageId;
    }
    public Integer getNoOfDay() {
        return NoOfDay;
    }
    public void setNoOfDay(Integer noOfDay) {
        NoOfDay = noOfDay;
    }
    public String getStatus() {
        return Status;
    }
    public void setStatus(String status) {
        Status = status;
    }
    public String getLastControlPoint() {
        return LastControlPoint;
    }
    public void setLastControlPoint(String lastControlPoint) {
        LastControlPoint = lastControlPoint;
    }
    public String getTypeOfTravel() {
        return TypeOfTravel;
    }
    public void setTypeOfTravel(String typeOfTravel) {
        TypeOfTravel = typeOfTravel;
    }
    public Date getLastEntryDate() {
        return LastEntryDate;
    }
    public void setLastEntryDate(Date lastEntryDate) {
        LastEntryDate = lastEntryDate;
    }
    public Date getReplyDateTime() {
        return ReplyDateTime;
    }
    public void setReplyDateTime(Date replyDateTime) {
        ReplyDateTime = replyDateTime;
    }
    @Override
    public String toString() {
        return "TravelHistoryResponseDTO [MessageId=" + MessageId + ", NoOfDay=" + NoOfDay + ", Status=" + Status
                + ", LastControlPoint=" + LastControlPoint + ", TypeOfTravel=" + TypeOfTravel + ", LastEntryDate="
                + LastEntryDate + ", ReplyDateTime=" + ReplyDateTime + "]";
    }
}

My code:我的代码:

    SOAPMessage message = MessageFactory.newInstance().createMessage(null,
                            new ByteArrayInputStream(responseString.getBytes()));
                    Unmarshaller unmarshaller = JAXBContext.newInstance(TravelHistoryResponseDTO.class).createUnmarshaller();
                    TravelHistoryResponseDTO dto = (TravelHistoryResponseDTO)unmarshaller.unmarshal(message.getSOAPBody().extractContentAsDocument());

But I am getting javax.xml.bind.UnmarshalException: unexpected element (uri: http://example.com/" , local:"submitCdus1Response"). Expected elements are <{}submitCdus1Response>但我得到 javax.xml.bind.UnmarshalException: 意外元素(uri: http://example.com/" , local:"submitCdus1Response")。预期的元素是 <{}submitCdus1Response>

Anyway to solve this and able to map the parameters to the java object?无论如何要解决这个问题并能够将 map 的参数传递给 java object?

There is an error in the JAXB object mapping: you are using @XmlAttribute for the various fields of your object, but the XML data you are trying to read are not XML attributes ( <ns0:submitCdus1Response MessageId="xxx"> ) but XML elements ( <MessageId>xxx</MessageId> ). There is an error in the JAXB object mapping: you are using @XmlAttribute for the various fields of your object, but the XML data you are trying to read are not XML attributes ( <ns0:submitCdus1Response MessageId="xxx"> ) but XML元素( <MessageId>xxx</MessageId> )。

Therefore all you have to is switch from @XmlAttribute to @XmlElement to match the mapping object to your actual XML.因此,您所要做的就是从@XmlAttribute切换到@XmlElement以匹配映射 object 到您的实际 XML。

As a note: if this comes really out of a SOAP call, chances are that you have a XSD Schema somewhere that defines the XML structure that is used here.注意:如果这真的来自 SOAP 调用,那么您很可能在某处有一个 XSD 架构,它定义了此处使用的 XML 结构。 JAXB comes with a code generation tool that will create the JAXB Objects (eg TravelHistoryResponseDTO ) for you, deriving the data from the XSD. JAXB 附带一个代码生成工具,该工具将为您创建 JAXB 对象(例如TravelHistoryResponseDTO ),从 XSD 派生数据。 The tool is called xjc (you can google / search for it), it comes with the JDK and or can be found in maven plugins, (and more).该工具称为xjc (您可以 google/搜索它),它带有 JDK,或者可以在 maven 插件中找到,(以及更多)。 This is the kind of mistake the tool would have prevented.这是该工具可以防止的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM