在Spring Boot应用程序中使用肥皂服务



我需要在Spring Boot中使用SOAP服务。我该如何使用像我们休息的那样轻松地做到这一点。我需要发送标头,形成身体以进行服务。请帮助我解决解决方案

public String sendMessage(String processInstanceId) {
        WebServiceTemplate webServiceTemplate = new WebServiceTemplate();
        String request = "<SOAP:Envelope xmlns:" + "SOAP='http://schemas.xmlsoap.org/soap/envelope/'>" + "<SOAP:Body>"
                + "<SendMessage xmlns='http://schemas.cordys.com/bpm/execution/1.0'>" + "<receiver>" + processInstanceId
                + "</receiver>" + "<message overwrite='false' />" + "</SendMessage>" + "</SOAP:Body>"
                + "</SOAP:Envelope>";
        SendMessageAPI sendMessageObject = new SendMessageAPI();
        StreamSource source = new StreamSource(new StringReader(request));
        StreamResult result = new StreamResult(System.out);
        System.out.println("called service" + request);
        webServiceTemplate.sendSourceAndReceiveToResult(
                "url",
                source, result);
        return "Success";

您可以使用春季Web服务,其中WebServiceTemplate类似于RestTemplate

为了添加SOAP标头和/或HTTP标头,您可以实现WebServiceMessageCallback接口。

在这里,一个简单的添加HTTP标头

的简单示例

WebServiceMessageCallback实现(请注意,我正在使用Axiom作为MessageFactory)

public class WsHttpHeaderCallback implements WebServiceMessageCallback
{
    private String headerKey;
    private String headerValue;
    private String soapAction;
    public WsHttpHeaderCallback(String headerKey, String headerValue, String soapAction)
    {
        super();
        this.headerKey = headerKey;
        this.headerValue = headerValue;
        this.soapAction = soapAction;
    }
    public WsHttpHeaderCallback()
    {
        super();
    }
    @Override
    public void doWithMessage(WebServiceMessage message) throws IOException, TransformerException
    {
        validateRequiredFields();
        addRequestHeader(headerKey, headerValue);
        if (StringUtils.hasText(this.soapAction))
        {
            AxiomSoapMessage axiomMessage = (AxiomSoapMessage) message;
            axiomMessage.setSoapAction(this.soapAction);
        }       
    }
    private void addRequestHeader(String headerKey, String headerValue)
    {
        TransportContext context = TransportContextHolder.getTransportContext();
        WebServiceConnection connection = context.getConnection();
        if (connection instanceof HttpComponentsConnection)
        {
            HttpComponentsConnection conn = (HttpComponentsConnection) connection;
            HttpPost post = conn.getHttpPost();
            post.addHeader(headerKey, headerValue); 
        }
        else if( connection instanceof ClientHttpRequestConnection )
        {
            ClientHttpRequestConnection conn = (ClientHttpRequestConnection)connection;
            conn.getClientHttpRequest().getHeaders().add(headerKey, headerValue);
        }
    }   
}

WebServiceMessageCallback用法:

WebServiceResponse resp = (WebServiceResponse)webSvcTemplate.marshalSendAndReceive(wsUrl, request, new WsHttpHeaderCallback(headerKey, headerValue, "http://ws.com/soapAction") );

我希望它有用

angelo