如何从SOAP WS中的URL访问查询参数?

时间:2018-10-10 06:38:44

标签: soap soapui

我想创建一个SOAP服务方法,该方法使用URL中的数据作为查询参数。但是我不确定,如何在SOAP URL中将数据作为查询参数传递。我创建了如下方法来接受数据,但这将来自SOAP请求: 还让我知道我们将如何通过SOAP UI在Query参数中传递数据:

@WebMethod
    public String test(String str){

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

    }

如果你们中有人帮助我,那将非常有帮助。预先感谢!

1 个答案:

答案 0 :(得分:0)

以下代码使用Servlet上下文获取查询参数。我提供了两种方法。第一种方法仅使用通过SOAP请求传递的数字。第二种方法处理传递的一个或多个查询参数,并给出两个访问查询参数的示例。

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

}

在SOAP UI中,创建新的SOAP项目后,它看起来像这样,我显示了两个示例(右窗格),第一个示例调用了Web服务方法,该方法只是将两个数字加在一起传递给SOAP主体作为参数。第二种方法(底部)首先获得一个查询参数,即使它们是两个,也将其添加到queryStringNumber1中,然后是第二个示例,该示例使用迭代器遍历参数映射,然后将所有传递的值添加到queryStringNumber1中。最后,它将肥皂输入变量添加到queryStringNumber1并返回该值。

您还可以使用诸如purpose of bindingprovider in jax-ws web service之类的绑定提供程序,而google提供了更多示例。

SOAP UI Example