Search code examples
javaweb-serviceshttp-headersjax-ws

JAX-WS sending empty value after explicitly adding SOAPAction header


I have made webservice client with NetBeans 8.2 wizard from an existing WSDL document. Java8 and JAX-WS 2.2.9 is used.

As far as I understand everything works with the wizard created code as expected but the query is missing value from "SOAPAction" header which is requirement to have value for the query to work. The header key exists but value is empty string: SOAPAction: "" when it should be SOAPAction = "SendReports"

I have tried using this:

Map<String, List<String>> requestHeaders = new HashMap<>();
requestHeaders.put("SOAPAction", Arrays.asList("sendReports"));
sourceDispatch.getRequestContext().put(MessageContext.HTTP_REQUEST_HEADERS, requestHeaders);

-> which results also in empty value. If I put "SOAPAction_2" & "sendReports", that header works correct but obviously the header key is wrong and wont solve my problem. Something overwrites my value afterwards?

The webservice method has annotation:

@WebMethod(operationName = "SendReports", action = "SendReports")

Any tips on what could I try next?

I saw many posts suggesting using BindingProvider but I cannot use com.sun.* packages for reasons left unexplained.


Solution

  • Finally found a working solution.

    I created a Web service Dispatch client with Netbeans (as originally) and had to add SOAPACTION_USE_PROPERTY and SOAPACTION_URI_PROPERTY. These I had tried before as System properties but seemed not to work that way.

    Here is the working snippet:

    public void sendReports() throws IOException {
        ReportService service = new ReportService();
        QName portQName = new QName("http://URL/ReportService", "ReportService");
        req = readFile("C:/temp/myFile.xml", "UTF-8");;
        try {
    
            // Call Web Service Operation
            Dispatch<Source> sourceDispatch = null;
            sourceDispatch = service.createDispatch(portQName, Source.class, Service.Mode.PAYLOAD);
    
            Map<String, Object> map = sourceDispatch.getRequestContext();
            map.put(BindingProvider.SOAPACTION_USE_PROPERTY, Boolean.TRUE);
            map.put(BindingProvider.SOAPACTION_URI_PROPERTY, "SendReports");
    
            Source result = sourceDispatch.invoke(new StreamSource(new StringReader(req)));
        } catch (Exception ex) {
            // TODO handle custom exceptions here
        }
    }