【发布时间】:2021-08-10 13:59:13
【问题描述】:
我正在尝试将 Soap 响应字符串解析为 JAVA 对象以获取这些参数。
这是响应字符串:
<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>
我的对象类:
@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 + "]";
}
}
我的代码:
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());
但我得到了 javax.xml.bind.UnmarshalException:意外元素(uri:http://example.com/",本地:“submitCdus1Response”)。预期元素是
无论如何解决这个问题并能够将参数映射到java对象?
【问题讨论】:
-
您的 XML 文件中是否缺少空格:
<S:Envelopexmlns:S="http://schemas.xmlsoap.org/soap/envelope/">?应该有一个空格分隔Envelope和xmlns。如果不是,则该文件不是有效的 XML,尝试使用标准工具对其进行解析没有任何意义。 (submitCdus1Responsexmlns相同)。更正后,您可能应该将@XMLRootElement注释中的命名空间设置为http://example.com/ -
xml 格式是我的错字,我已经编辑了这篇文章,谢谢。我厌倦了将命名空间包含到
@XmlRootElement(name="submitCdus1Response", namespace="http://example.com/" )但对象的值仍然为空。
标签: java xml soap unmarshalling