How to add parameters to SOAP URL and retrieve them service side

10,162

As a web service provider you can access the query string used by the SOAP client via the HttpServletRequest in the MessageContext:

package org.example.sampleservice;

import javax.annotation.Resource;
import javax.jws.HandlerChain;
import javax.jws.WebService;
import javax.servlet.http.HttpServletRequest;
import javax.xml.ws.WebServiceContext;
import javax.xml.ws.handler.MessageContext;

@WebService(endpointInterface = "org.example.sampleservice.SampleService")
public class SampleServiceImpl implements SampleService {

    @Resource
    private WebServiceContext ctx;

    @Override
    public String sayHello(String name) {
        HttpServletRequest request = (HttpServletRequest) ctx.getMessageContext().get(MessageContext.SERVLET_REQUEST);

        String result = String.format("Hello, %s (invoked with endpoint query parameters %s)", name,
                request.getQueryString() == null ? "[no endpoint URL query parameters found]"
                        : request.getQueryString());
        return result;
    }

}

You can either get the query string as one string as I have above (request.getQueryString()) or via other standard HttpServletRequest methods:

Example soap client for this class:

package org.example.consumer;

import java.net.URL;

import javax.xml.ws.BindingProvider;

import org.example.sampleservice.SampleService;
import org.example.sampleservice.SampleServiceImplService;

public class SayHelloClientApp {

    public static void main(String[] args) throws Exception {
        URL wsdlLoc = new URL("http://localhost:8081/samplews/sample?WSDL");
        SampleServiceImplService svc = new SampleServiceImplService(wsdlLoc);
        SampleService port = svc.getSampleServiceImplPort();

        BindingProvider bp = (BindingProvider) port;
        String endpointURL = "http://localhost:8081/samplews/sample?a=1&b=2&c=3";
        bp.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, endpointURL);

        String result = port.sayHello("java");
        System.out.println(String.format("Result:\n%s", result));
    }

}

Prints

Result: Hello, java (invoked with endpoint query parameters a=1&b=2&c=3)

Share:
10,162

Related videos on Youtube

kbreezy04
Author by

kbreezy04

Updated on September 15, 2022

Comments

  • kbreezy04
    kbreezy04 almost 2 years

    I am using jaxws-rt and have WSDLs built and the web services all created. Everything works fine but I am wonder if there is a way to see if more parameters were tagged onto the URL from the web service.

  • kbreezy04
    kbreezy04 over 8 years
    Thanks, I will give this a try and see if I can make this work for what I need.
  • kbreezy04
    kbreezy04 over 8 years
    This is exactly what I wanted, will mark as best answer.