2010-10-28 20 views
11

Jak mogę dodać nagłówki SOAP do klienta Spring Jax-WS?Jak dodać nagłówki SOAP do klienta Spring Jax-WS?

W szczególności, mam obiekt Jaxb, który chciałbym dodać do nagłówka, ale przykłady xml byłyby docenione.

Używam Spring's JaxWsPortProxyFactoryBean opisanego jako here. Ponadto generuję klienta zgodnie z opisem here, który działa mniej nagłówków, które muszę dodać.

Dziękuję.

Odpowiedz

6

Nadal staram się znaleźć elegancki sposób dodawania nagłówków, ale to, co robię, jak sugerują inni, to użycie transformatora w WebServiceMessageCallBack(). Oto przykładowy kod:

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); 
     } 
} 
... 

To nie jest zbyt eleganckie, biorąc pod uwagę, że jest to WS na wiosnę. To nie jest intuicyjne.

14

trochę bardziej elegancki (nadal wymagana jest obsada klasa):

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 
    } 
} 

Uwaga: Ten przykład został jąder z wiosennym WS 2.1.4.

5

Po pewnym poklepaniu, jeśli znaleziono nieco inne rozwiązanie. Używam JAXB do porządkowania mojego ładunku, a możliwe klasy nagłówków zostały również wygenerowane za pomocą JAXB z WSDL. W moim przypadku zajmuję się usługami Microsoft Reporting Services i przekazuję identyfikator wykonawczy jako nagłówek 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)); 
    } 
} 

Zasadniczo WebServiceGatewaySupport zna już program Marshaller do konwersji JAXB Pojos. Używam tego jednego, aby dołączyć własne zajęcia nagłówka do SoapHeader z tej linii:

getMarshaller().marshal(executionHeader, soapMessage.getSoapHeader().getResult()); 

w moim zagnieżdżonego WebServiceMessageCallback.

Powiązane problemy