如何将SOAP标头添加到Spring Jax-WS客户端?

Bra*_*don 13 java spring jax-ws soapheader

如何将SOAP标头添加到Spring Jax-WS客户端?

具体来说,我有一个Jaxb对象,我想添加到标题,但xml示例将不胜感激.

我正在使用这里描述的Spring的JaxWsPortProxyFactoryBean .此外,我正在生成我的客户端,如此处所述,它减少了我需要添加的标头.

谢谢.

use*_*523 15

更优雅一点(仍然需要一个类演员):

public void doWithMessage(WebServiceMessage message) {
    try {
        SOAPMessage soapMessage = ((SaajSoapMessage)message).getSaajMessage();
        SOAPHeader header = soapMessage.getSOAPHeader();
        SOAPHeaderElement security = header.addHeaderElement(new QName("http://schemas.xmlsoap.org/ws/2003/06/secext", "Security", "wsse"));
        SOAPElement usernameToken = security.addChildElement("UsernameToken", "wsse");
        SOAPElement username = usernameToken.addChildElement("Username", "wsse");
        SOAPElement password = usernameToken.addChildElement("Password", "wsse");

        username.setTextContent(someUsername);
        password.setTextContent(somePassword);
    } catch (Exception e) {
       //... handle appropriately
    }
}
Run Code Online (Sandbox Code Playgroud)

注意:此示例是使用Spring WS 2.1.4的测试.


chr*_*ris 6

我仍然试图找到一种优雅的方式来添加标题,但我按照其他人的建议做的是在WebServiceMessageCallBack()上使用Transformer.这是一个示例代码:

JAXBElement<GetDeletedResponse> result = (JAXBElement<GetDeletedResponse>) webServiceTemplate.marshalSendAndReceive(request, new WebServiceMessageCallback() {
public void doWithMessage(WebServiceMessage webServiceMessage) {
    try {
        SoapMessage soapMessage = (SoapMessage) webServiceMessage;
        soapMessage.setSoapAction("getDeleted");

        SoapHeader header = soapMessage.getSoapHeader();
        StringSource headerSource = new StringSource("<account>\n" +
                                "<username>"+"johnsmith"+"</username>\n" +
                                "<password>"+"1234"+"</password>\n" +
                                "</account>");
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.transform(headerSource, header.getResult());

       } catch (Exception e) {
         new RuntimeException(e);
       }
}
...
Run Code Online (Sandbox Code Playgroud)

考虑到这是Spring的WS,它并不是很优雅.这不直观.


Wil*_*sau 6

如果找到一个稍微不同的解决方案,经过一些戳.我正在使用JAXB来编组我的有效负载,并且还使用来自WSDL的JAXB生成了可能的头类.在我的情况下,我正在解决Microsoft Reporting Services并将ExecutionID作为SOAP标头传递.

public class ReportExecution2005Client extends WebServiceGatewaySupport {

    private static final String SET_EXECUTION_PARAMETERS_ACTION = "http://schemas.microsoft.com/sqlserver/2005/06/30/reporting/reportingservices/SetExecutionParameters";

    private final class SoapActionExecutionIdCallback implements WebServiceMessageCallback {

        private final String soapAction;
        private final String executionId;

        public SoapActionExecutionIdCallback(String soapAction, String executionId) {
            super();
            this.soapAction = soapAction;
            this.executionId = executionId;
        }

        @Override
        public void doWithMessage(WebServiceMessage message) throws IOException, TransformerException {
            SoapMessage soapMessage = (SoapMessage) message;
            soapMessage.setSoapAction(soapAction);
            ExecutionHeader executionHeader = new ExecutionHeader();
            executionHeader.setExecutionID(executionId);
            getMarshaller().marshal(executionHeader, soapMessage.getSoapHeader().getResult());
        }
    }

    public void setExecutionParameters(String executionId){
        SetExecutionParameters request = new SetExecutionParameters();
        request.setParameters(new ArrayOfParameterValue());

        SetExecutionParametersResponse response = (SetExecutionParametersResponse) getWebServiceTemplate().marshalSendAndReceive(request,
                new SoapActionExecutionIdCallback(
                        SET_EXECUTION_PARAMETERS_ACTION,
                        executionId));
    }
}
Run Code Online (Sandbox Code Playgroud)

基本上,WebServiceGatewaySupport已经知道要转换JAXB Pojos的Marshaller.我正在使用这个将我自己的头类添加到SoapHeader中:

getMarshaller().marshal(executionHeader, soapMessage.getSoapHeader().getResult());
Run Code Online (Sandbox Code Playgroud)

在我的嵌套WebServiceMessageCallback中.