如何为本地文件指定 wsdlLocation

How to specify wsdlLocation for local file

我是 Web 服务客户端游戏的新手,并且使用 wsdl2java maven 依赖项生成了代码。

我将此项目打包为.war 文件。

我的问题是我不知道如何让自动生成的客户端跨域到达端点,我也不知道如何正确设置客户端的 wsdlLocation。

从后者开始: 在自动生成的服务 class 中,我发现了一个 static URL WSDL_LOCATION 属性,以及 WebServiceClient 注释的 'parameters?'。

@WebServiceClient(name = "my_service", 
                  wsdlLocation = "file:/c:/tmp/my_service.wsdl",
                  targetNamespace = "someAutoGenTargetNS/wsdl") 
public class my_service_Service extends Service {

    public final static URL WSDL_LOCATION;

    public final static QName SERVICE = new QName("someAutoGenTargetNS/wsdl", "my_service");
    public final static QName my_service = new QName("http://someAtuoGenTargetNS/wsdl", "my_service");
    static {
        URL url = null;
        try {
            url = new URL("file:/c:/tmp/my_service.wsdl");
        } catch (MalformedURLException e) {
            java.util.logging.Logger.getLogger(my_service_Service.class.getName())
                .log(java.util.logging.Level.INFO, 
                     "Can not initialize the default wsdl from {0}", "file:/c:/tmp/my_service.wsdl");
        }
        WSDL_LOCATION = url;
    }

目前,WSDL_LOCATION url 始终设置为空,因为它找不到指定的文件路径(这是预期的)。我将 wsdl 和 xsd 存储在资源文件夹中,但不知道如何指定到达该路径的路径。我试过了

new URL("file:/resources/my_service.wsdl")
new URL("file:/src/main/java/resources/my_service.wsdl")
new URL("classpath:my_service.wsdl")

还有很多其他人。执行此操作的正确方法是什么,如果它需要 xml-目录,我在哪里可以找到有关放置 catalog.xml 文件的位置的文档。

现在是前者: 我相信我将端点更改为我想要的位置的实现是正确的,而让我头疼的是 wsdlLocation 问题。这是我更改端点的实现。如果这看起来不对,只需指出使用方向即可,无需完整实施。

private static final QName SERVICE_NAME = new QName(""someAutoGenTargetNS/wsdl"", "my_service");
    private URL wsdlURL = my_service_Service.WSDL_LOCATION;
    my_service_Service ss;
    my_service port;
public my_service_client()
{
        //Redacted code for trusting all SSL Certs and hostnameVerifiers as it is not needed for the scope of this question
        this.ss = new my_service_Service(this.wsdlURL,SERVICE_NAME);
        this.port = ss.getMy_Service();
        BindingProvider bp = (BindingProvider) this.port;
        Map<String,Object> clientRequestContext = bp.getRequestContext();
        clientRequestContext.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "https://New_Endpoint_IP");
//Basic auth header credentials
        clientRequestContext.put(BindingProvider.USERNAME_PROPERTY,"username");
        clientRequestContext.put(BindingProvider.PASSWORD_PROPERTY,"password");
}

在问这个问题之前我试图了解的资源:

1 2 3 4 5 6 7+ 8

最后,我开始意识到我更愿意使用 HTTP 而不是 SOAP,但是遗留系统需要遗留交互。

收到的错误让我相信这是一个 wsdlURL 问题:

javax.xml.ws.soap.SOAPFaultException: Internal Error (from client)
org.apache.cxf.binding.soap.SoapFault: Internal Error (from client)

解决了问题(4天后跟踪问题,我在这里问问题后很快就能解决...)

这确实是设置端点的有效方法URL。这回答了这个问题。

我采用了这个reference中的classLoader.getResource方法来设置wsdlLocation

我的新代码变成了:

@WebServiceClient(name = "my_service", 
                  wsdlLocation = "classpath:my_service.wsdl",
                  targetNamespace = "someAutoGenTargetNS/wsdl") 
public class my_service_Service extends Service {

    public final static URL WSDL_LOCATION;

    public final static QName SERVICE = new QName("someAutoGenTargetNS/wsdl", "my_service");
    public final static QName my_service = new QName("http://someAtuoGenTargetNS/wsdl", "my_service");
    static {
        URL url = null;
            url = my_service.class.getClassLoader().getResource("my_service.wsdl");
        WSDL_LOCATION = url;
    }

最后我意识到我尝试使用的最基本的 web-service 调用错误地设置了请求正文。我已将 request_body 设置为 null,而不是实例化自动生成的请求 class 的新实例。这就是引发这两个错误的原因,但我确信解决上述问题有助于我理解这些部分如何协同工作,从而帮助我找到解决方案。