Rohit
Rohit

Reputation: 450

How to access query param from url in SOAP WS?

I want to create a SOAP service method which consume data from URL as query param. But i am not sure, How can we pass data as query param in SOAP URL. I have created the method as below accepting data but that will come from SOAP request: Also let me know how we would be passing data in the Query param from SOAP UI:

@WebMethod
    public String test(String str){

        System.out.println("Test method called:"+ str);
        return str;

    }

It would be very helpful if any of you help me out. Thanks in advance!

Upvotes: 1

Views: 4327

Answers (1)

JGlass
JGlass

Reputation: 1467

The following code uses the Servlet context to obtain the query param(s). I've provided two methods. The first method just uses the numbers passed via the SOAP request. The second method deals with one or more query parameters passed and gives two examples of accessing the query parameters.

package net.myco.ws;

import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

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

@WebService
public class SOAPWSWithQueryParam {

    @Resource
    private WebServiceContext ctx;

    /**
     * Default no arg constructor
     */
    public SOAPWSWithQueryParam() {
        super();
    }

    /*
     * Web Service that adds two numbers together
     */
    @WebMethod
    public int addTwoNumbers(
            @WebParam(name="inputNumber1") int inputNumber1,
            @WebParam(name="inputNumber2") int inputNumber2
            ){

        int result;
        result = inputNumber1 + inputNumber2;
        return result;
    }

    /*
     * Web Service that adds two numbers together, *but* also inspects
     * the HTTP POST for a single queryParam and adds that as well.
     * 
     * Example URL:
     * http://localhost:8080/SOAPWSWithQueryParam/SOAPWSWithQueryParam?number1=8&number2=6
     * 
     * Note, we're only getting the first query param, we could split based on "&" and get
     * other params.
     * 
     */
    @WebMethod
    public int addThreeNumbers(
            @WebParam(name="inputNumber1") int inputNumber1,
            @WebParam(name="inputNumber2") int inputNumber2
            ){

        int result;
        int queryStringNumber1 = 0;
        Map <String, String[]>quesryStringMap;

        HttpServletRequest servletRequest = (HttpServletRequest) ctx.getMessageContext().get(MessageContext.SERVLET_REQUEST);

        /*
         * Likely want to add a try catch on this or other logic in case there isn't a query string param. 
         * Also, because the example URL contains a second param, we split again at the "&" in URL else the 
         * result would be "8&number2"
         */
        queryStringNumber1 = Integer.valueOf(servletRequest.getQueryString().split("=")[1].split("&")[0]);

        /*
         * The second and more elegant way of accomplishing it is using the Parameters Map, because we're
         * adding the second way of doing it, the returned value is increased as it was 17 based on our URL 
         * and the WS two input numbers.  Now it becomes 31.
         * 
         */
        quesryStringMap = servletRequest.getParameterMap();
        Iterator<Entry<String, String[]>> mapIterator = quesryStringMap.entrySet().iterator();
        while (mapIterator.hasNext()) {
            Map.Entry<String, String[]> pair = (Entry<String, String[]>)mapIterator.next();
            System.out.println(pair.getKey() + " = " + pair.getValue()[0]);
            /*
             * Prints:
             07:43:57,666 INFO  [stdout] (default task-10) number1 = 8
             07:43:57,666 INFO  [stdout] (default task-10) number2 = 6
             */

            //Add the other param values
            queryStringNumber1 += Integer.valueOf(pair.getValue()[0]);

            mapIterator.remove();
        }

        result = inputNumber1 + inputNumber2 + queryStringNumber1;

        return result;
    }

}

From SOAP UI, after you have created a new SOAP project it would look like this, I've shown two examples (right pane), the first example calls the web service method which just adds the two numbers together passed in the SOAP body as arguments. The second method (bottom) first gets a single query parameter even though their are two, adds that to queryStringNumber1 it then has a second example which uses a iterator to iterate through the parameter map and then adds any passed values to queryStringNumber1. Finally, it adds the soap input variables to queryStringNumber1 and returns that value.

You could also uses the Binding Provider such as purpose of bindingprovider in jax-ws web service and google provides even more examples.

SOAP UI Example

Upvotes: 1

Related Questions