【问题标题】:RestTemplate and soap unmarshallerRestTemplate 和肥皂解组器
【发布时间】:2014-07-15 00:16:04
【问题描述】:

我正在尝试使用 moxy 来解组肥皂响应,例如:

<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:b2bHotelSOAP" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
   <SOAP-ENV:Body>
      <ns1:getAvailableHotelResponse>
         <return xsi:type="ns1:getAvailableHotelResponse">
            <responseId xsi:type="xsd:integer">1</responseId>
            <searchId xsi:type="xsd:string">HR-47754204</searchId>
            <totalFound xsi:type="xsd:integer">20</totalFound>
            <availableHotels SOAP-ENC:arrayType="ns1:hotel[20]" xsi:type="ns1:hotelArray">
               ...
            </availableHotels>
          </return>
      </ns1:getAvailableHotelResponse>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

这是我的豆子:

@XmlRootElement(name = "getAvailablegetAvailableHotelResponse")
public class MyBean{

    @XmlPath("return/availableHotels/item")
    private List<Hotel> hotels;

    public List<Hotel> getHotels(){
        return this.hotels==null?new ArrayList<Hotel>():this.hotels;
    }
}

使用简单的 xml 字符串,我正确地将 xml 转换为 bean,但在这个肥皂盒中我不知道该怎么做。

为了获得响应,我以这种方式使用 RestTemplate:

MyBean response = template.postForObject(endpoint.toURI(), entity, MyBean.class);

我收到了这个例外:

org.springframework.http.converter.HttpMessageNotReadableException: Could not unmarshal to 
[class myPackage.MyBean]: unexpected element 
(uri:"http://schemas.xmlsoap.org/soap/envelope/", local:"Envelope"). Expected elements are 
<{}getAvailablegetAvailableHotelResponse>; nested exception is 
javax.xml.bind.UnmarshalException: unexpected element 
(uri:"http://schemas.xmlsoap.org/soap/envelope/", local:"Envelope"). Expected elements are 
<{}getAvailablegetAvailableHotelResponse>

谁能给我解释一个好的解决方案?

谢谢!

【问题讨论】:

  • 不要使用RestTemplate,因为这不是休息服务,而是肥皂服务。使用 Spring-WS 中的 WebServiceTemplate

标签: xml spring soap jaxb moxy


【解决方案1】:

请不要混合使用 SOAP 和 REST。

尝试使用 Spring WebServices 项目中的 WebServiceTemplate 解决您的问题,或者在将 XML 响应解组到域对象之前对其进行一些 XPath 转换 (//getAvailableHotelResponse)。

这是一个简单的例子:

WebServiceTemplate template = new WebServiceTemplate(marshaller, unmarshaller);
template.marshalSendAndReceive(endpoint.toURI(), entity);

【讨论】:

  • 能否请您提供一个简单的使用 WebServiceTemplate 或 xPath 转换来实现我的目标?
  • 在我的回答中添加了更多信息
  • WebServiceTemplate 类型中的 marshalSendAndReceive(String, Object) 方法不适用于参数 (URI, HttpEntity) -> HttpEntity 包含我的soap请求作为xml字符串
  • 对,当然。您应该只发送您的域对象而不是其 xml 表示并用 SOAP 包装。 WebServiceTemplate 及其 marshaller 进行了正确的转换。
  • 所以我需要创建一个请求 bean 来包装编组器将转换为正确 xml 表示的所有信息?如果我需要这样做,我更喜欢在字符串上工作.. 所以我想我会尝试一种使用 restTemplate 来解组soapresponse的方法,也许将rootelement声明为soap信封
猜你喜欢
  • 2014-09-05
  • 2017-11-24
  • 1970-01-01
  • 1970-01-01
  • 2011-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多