如何将参数添加到SOAP URL并检索它们的服务端

时间:2015-10-20 23:32:29

标签: java jax-ws

我正在使用jaxws-rt并构建了WSDL并创建了所有Web服务。一切正常但我很想知道是否有办法查看是否有更多参数被标记到网络服务的URL上。

1 个答案:

答案 0 :(得分:4)

作为Web服务提供者,您可以通过HttpServletRequest中的MessageContext访问SOAP客户端使用的查询字符串:

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

}

您可以将查询字符串作为我上面的一个字符串(request.getQueryString())或通过其他标准HttpServletRequest方法获取:

此类的肥皂客户端示例:

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

}

打印

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