在Spring WS中将自定义元素添加到SOAP标头

坏处

我正在使用带有JAXB绑定的Spring WS对Web服务客户端应用程序进行编码。我正在使用的服务需要通过SOAP标头(以及另一个自定义标头)中的wsse:security元素进行身份验证。我已经编译了所有必需的模式,包括wsse.xsd编译为org.xmlsoap.schemas.ws._2002._12.secext.Security

但是我找不到将这种或任何其他元素插入SOAP标头中的方法。我知道我可以使用拦截器或SoapActionCallbacks,但是它们允许我做的是手动构造标头,然后通过((SaajSoapMessage)webServiceMessage).getSoapHeader().addHeaderElement(qName)诸如此类将其添加到标头部分但是我不想手动构建此标头,因为我有一个可以轻松编组的对应类。

我的问题-在Spring WS中调用Web服务调用时,是否可以将对象插入SOAP头(或信封的其他部分)中?我使用Apache CXF和Axis2来使用Web服务,但这从来都不是问题-框架只是在幕后为我(通常是通过服务存根机制)为我做的。

坏处

我已经设法解决了这个问题,感谢@GPI的提示。我对Spring WS和javax.xml。还是相当陌生,所以我无法确定这是正确的方法还是优雅的方法,但它确实满足我的要求。

这段代码<SOAP-ENV:Header>基于我通过JAXB从XSD架构生成的对象,向中添加了自定义标头元素我不知道Transformer如何知道我要将这些元素放置在哪里,但是它正确地将它们放置在Header部分中。

public class HeaderComposingCallback implements WebServiceMessageCallback {

    private final String action;

    public HeaderComposingCallback( String action ) {
        this.action = action;
    }

    @Override
    public void doWithMessage(WebServiceMessage webServiceMessage) throws IOException, TransformerException {

    SoapHeader soapHeader = ((SoapMessage)webServiceMessage).getSoapHeader();

    try {
        JAXBContext context = JAXBContext.newInstance( MessageHeader.class, Security.class );

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();

        Document securityDocument = builder.newDocument();
        Document headerDocument = builder.newDocument();

        Marshaller marshaller = context.createMarshaller();
        marshaller.marshal( HeaderFactory.getHeader( action ), headerDocument );
        marshaller.marshal( SecurityFactory.getSecurity(), securityDocument );

        Transformer t = TransformerFactory.newInstance().newTransformer();

        DOMSource headerSource = new DOMSource( headerDocument );
        DOMSource securitySource = new DOMSource( securityDocument );

        t.transform( headerSource, soapHeader.getResult() );
        t.transform( securitySource, soapHeader.getResult() );

    } catch (JAXBException | ParserConfigurationException e) {
        e.printStackTrace();
    }
}

}

然后,我只是在服务调用期间HeaderComposingCallback对象传递marshalSendAndReceive()方法。

编辑(在Arjen评论后)

阿延的权利。我想做的事情可以更简单地实现。现在我的doWithMessage方法如下:

    @Override
    public void doWithMessage(WebServiceMessage webServiceMessage) throws IOException, TransformerException {

    SoapHeader soapHeader = ((SoapMessage)webServiceMessage).getSoapHeader();

    try {
        JAXBContext context = JAXBContext.newInstance( MessageHeader.class, Security.class );

        Marshaller marshaller = context.createMarshaller();
        marshaller.marshal( header, soapHeader.getResult() );
        marshaller.marshal( security, soapHeader.getResult() );

    } catch (JAXBException e) {
        e.printStackTrace();
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在Spring集成中将X附加到自定义请求标头

来自分类Dev

如何将自定义ApplicationContextInitializer添加到Spring Boot应用程序?

来自分类Dev

将自定义UserDetailsService添加到Spring Security OAuth2应用

来自分类Dev

Spring Data Rest将自定义端点添加到特定存储库

来自分类Dev

如何将自定义搜索链接添加到Spring Data Rest

来自分类Dev

将自定义Thymeleaf模板解析器添加到Spring Boot

来自分类Dev

将自定义JVM参数添加到Spring Boot生成的bat / sh脚本

来自分类Dev

使用Spring DI将自定义对话框添加到Javafx

来自分类Dev

SoapUI-自动将自定义SOAP标头添加到传出请求中

来自分类Dev

将自定义标头添加到“请求”

来自分类Dev

将自定义标头添加到“请求”

来自分类Dev

Spring Jpa将自定义功能添加到所有存储库,同时将其他自定义功能添加到单个存储库

来自分类Dev

将自定义HttpMessageConverter添加到spring-boot / spring-data-rest应用程序

来自分类Dev

如何在SOAP标头spring-ws中添加xmlbean文档元素

来自分类Dev

如何在Spring-Integration中将SOAPAction标头添加到HTTP消息中?

来自分类Dev

将自定义http标头添加到所有jQuery AJAX请求中

来自分类Dev

将自定义http标头添加到chai请求

来自分类Dev

将自定义标头添加到目录中的特定文件

来自分类Dev

将自定义标头添加到.Net中的所有Swagger响应中

来自分类Dev

将自定义标头添加到REST PUT请求

来自分类Dev

Angular:将自定义HTTP响应标头添加到dev`ng serve`

来自分类Dev

如何将自定义标头添加到SignalR的Typescript客户端?

来自分类Dev

将自定义HTTP标头添加到Nginx X-Accel-Redirect

来自分类Dev

如何将自定义标头添加到Google oauth回调请求

来自分类Dev

将自定义标头添加到.Net中的所有Swagger响应中

来自分类Dev

将自定义标头添加到所有模块中的所有资源

来自分类Dev

将自定义标头添加到iframe asp.net

来自分类Dev

Wordpress将自定义HTTP标头添加到一个模板文件

来自分类Dev

Spring是将自定义Http状态,标头和正文返回给Rest Client的最简单方法

Related 相关文章

  1. 1

    在Spring集成中将X附加到自定义请求标头

  2. 2

    如何将自定义ApplicationContextInitializer添加到Spring Boot应用程序?

  3. 3

    将自定义UserDetailsService添加到Spring Security OAuth2应用

  4. 4

    Spring Data Rest将自定义端点添加到特定存储库

  5. 5

    如何将自定义搜索链接添加到Spring Data Rest

  6. 6

    将自定义Thymeleaf模板解析器添加到Spring Boot

  7. 7

    将自定义JVM参数添加到Spring Boot生成的bat / sh脚本

  8. 8

    使用Spring DI将自定义对话框添加到Javafx

  9. 9

    SoapUI-自动将自定义SOAP标头添加到传出请求中

  10. 10

    将自定义标头添加到“请求”

  11. 11

    将自定义标头添加到“请求”

  12. 12

    Spring Jpa将自定义功能添加到所有存储库,同时将其他自定义功能添加到单个存储库

  13. 13

    将自定义HttpMessageConverter添加到spring-boot / spring-data-rest应用程序

  14. 14

    如何在SOAP标头spring-ws中添加xmlbean文档元素

  15. 15

    如何在Spring-Integration中将SOAPAction标头添加到HTTP消息中?

  16. 16

    将自定义http标头添加到所有jQuery AJAX请求中

  17. 17

    将自定义http标头添加到chai请求

  18. 18

    将自定义标头添加到目录中的特定文件

  19. 19

    将自定义标头添加到.Net中的所有Swagger响应中

  20. 20

    将自定义标头添加到REST PUT请求

  21. 21

    Angular:将自定义HTTP响应标头添加到dev`ng serve`

  22. 22

    如何将自定义标头添加到SignalR的Typescript客户端?

  23. 23

    将自定义HTTP标头添加到Nginx X-Accel-Redirect

  24. 24

    如何将自定义标头添加到Google oauth回调请求

  25. 25

    将自定义标头添加到.Net中的所有Swagger响应中

  26. 26

    将自定义标头添加到所有模块中的所有资源

  27. 27

    将自定义标头添加到iframe asp.net

  28. 28

    Wordpress将自定义HTTP标头添加到一个模板文件

  29. 29

    Spring是将自定义Http状态,标头和正文返回给Rest Client的最简单方法

热门标签

归档