春季在SOAP标头中添加两个元素 [英] Spring adding two elements to the SOAP header

查看:73
本文介绍了春季在SOAP标头中添加两个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题基本上与这个问题.
我的信誉不足,无法在OP的问题中添加评论.请帮助

My question basically is the same as this question.
I don't have enough reputation to add a comment to the OP's question. Please help

我遇到的问题是:
我尝试调用的 SOAP Web服务要求标头包含两个元素,一个包含基本标头数据,另一个包含同步特定数据,所需的标头如下所示:

The problem I'm having is:
The SOAP web service I'm trying to call requires the header to have two elements, one containing basic header data and another with synchronisation specific data, the header that's required looks like this :

<header>
  <initHeader>
    <requestID></requestId>
    <...some more elements>
  </initHeader>
 <syncHeader>
    <appId></appId>
    <dateTime></dateTime>
    <event></event>
 </syncHeader>
</header>

使用WebServiceMessageCallback生成标头时(特别是在下面显示的转换过程中),我得到了:

When generating the header using WebServiceMessageCallback (specifically during the transformation shown below), I get this:

"ERROR: 'The markup in the document following the root element must be well-formed.'"

    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.transform(new StringSource(soapHeaderStr), ((SoapMessage) message).getSoapHeader().getResult());

这里的问题是转换器希望标头中的所有元素都在一个根元素下.但是这里的标头有两个.

The issue here is that the transformer expects all the elements in the header to be under one root element. But here, the header has two.

我这样更改了标头数据(如下),并且转换器没有抱怨.

I changed the header data like this(below) and the transformer does not complain.

    <header>
      <myRootelement>
      <initHeader>
        <requestID></requestId>
        <...some more elements>
      </initHeader>
     <syncHeader>
        <appId></appId>
        <dateTime></dateTime>
        <event></event>
     </syncHeader>
     </myRootelement>
    </header>

根据上述问题,OP通过如上所述添加一个虚拟根元素并在将其转换为标头之前将其删除来解决了该问题.

According to the above mentioned question, the OP had solved this problem by addigng a dummy root element as above and then removing it just before transforming it into the header.

我想知道如何去除虚拟根元素?这样的事情可能是? http ://technology.amis.nl/2011/05/16/how-to-remove-unwanted-soap-header-elements-in-jax-ws/
我不太确定如何在保持子元素完好无损的情况下删除根元素.

I want to know how this removal of the dummy root elements is possible ? Something like this may be ? http://technology.amis.nl/2011/05/16/how-to-remove-unwanted-soap-header-elements-in-jax-ws/
I'm not quite sure how to remove a root element while keeping its children intact.

推荐答案

设法解决问题,方法如下:

Managed to solve the problem, Here's how :

而不是将两个元素编组到StringResult对象中,然后尝试使用像这样的Transformer将它们添加到标头中:

Instead of marshalling the two elements into StringResult objects and then trying to add them to the header using a the Transformer like this :

StringResult stringResult = new StringResult();
webServiceTemplate.getMarshaller().marshal(element, stringResult);
StringSource headerSource = new StringSource(stringResult.toString());
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.transform(headerSource, soapHeader.getResult());

您可以将两个元素直接编组到肥皂头中,如下所示:

You can marshal the two elements directly into the soap header like this :

webServiceTemplate.getMarshaller().marshal(element1,soapHeader.getResult());
webServiceTemplate.getMarshaller().marshal(element2,soapHeader.getResult());

此处提到的编组器是"org.springframework.oxm.jaxb.Jaxb2Marshaller" 上面的元素1和2是使用生成的Object工厂类创建的JAXB元素.

The marshaller mentioned here is a "org.springframework.oxm.jaxb.Jaxb2Marshaller" The element 1 and 2 above are the JAXB elements created using the generated Object factory class.

使用这种方法,无需添加虚拟根元素.

With this approach, there is no need to add dummy root elements.

希望这对外面的人有帮助,并感谢Grigori.G为我指明了正确的方向!

Hope this helps someone out there, and thanks to Grigori.G for pointing me in the right direction !

这篇关于春季在SOAP标头中添加两个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆