如何将Spring Ws中的Soap请求消息发送到接受Soap请求消息的服务器?

时间:2016-05-12 18:53:35

标签: java web-services spring-mvc soap cxf

如何使用Spring WS配置Spring MvC 4应用程序以与Bank等第三方建立双向SSL连接。我需要交换证书并验证服务器证书然后交换Soap消息。如果有任何链接或代码或程序请更新

2 个答案:

答案 0 :(得分:3)

Spring-WS可以实现。您必须使用Spring WS提供的WebServiceTemplate API。以下是使用它的方法。

import java.io.StringReader;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.springframework.ws.WebServiceMessageFactory;
import org.springframework.ws.client.core.WebServiceTemplate;
import org.springframework.ws.transport.WebServiceMessageSender;

public class WebServiceClient {

    private static final String MESSAGE =
        "<message xmlns=\"http://tempuri.org\">Hello Web Service World</message>";

    private final WebServiceTemplate webServiceTemplate = new WebServiceTemplate();

    public void setDefaultUri(String defaultUri) {
        webServiceTemplate.setDefaultUri(defaultUri);
    }

    // send to the configured default URI
    public void simpleSendAndReceive() {
        StreamSource source = new StreamSource(new StringReader(MESSAGE));
        StreamResult result = new StreamResult(System.out);
        webServiceTemplate.sendSourceAndReceiveToResult(source, result);
    }

    // send to an explicit URI
    public void customSendAndReceive() {
        StreamSource source = new StreamSource(new StringReader(MESSAGE));
        StreamResult result = new StreamResult(System.out);
        webServiceTemplate.sendSourceAndReceiveToResult("http://localhost:8080/AnotherWebService",
            source, result);
    }

}

请参阅此link

另一个有用的link2

答案 1 :(得分:2)

使用here in the JSSE doc所述的密钥库和信任库的必要属性启动您的VM:

 -Djavax.net.ssl.keyStoreType=yourKeystoreType(jks,pkcs12,etc.)
 -Djavax.net.ssl.trustStoreType=yourTruststoreType(jks,pkcs12,etc.)  
 -Djavax.net.ssl.keyStore=/path/to/your/keystore  
 -Djavax.net.ssl.trustStore=/path/to/your/truststore
 -Djavax.net.debug=ssl  
 -Djavax.net.ssl.keyStorePassword=...  
 -Djavax.net.ssl.trustStorePassword=...

然后在您的客户端代码中,您需要获取SSLSocketFactory

SSLSocketFactory sslsocketfactory =(SSLSocketFactory) SSLSocketFactory.getDefault();

并将其附加到WebServiceTemplate(用于创建取自ClientCustomSSL from apache examples的HttpClient的代码段):

WebServiceMessageSender sender = new HttpComponentsMessageSender(HttpClients.custom().setSSLSocketFactory(sslsocketfactory ));
getWebServiceTemplate().setMessageSender(sender);