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

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

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

作为 Web 服务提供商,您可以通过 MessageContext 中的 HttpServletRequest 访问 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 方法获取:

此 class 的 soap 客户端示例:

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)