首页 > 解决方案 > 在 AWS Lambda 中运行时从资源加载 WSDL 时出错

问题描述

我导入了一个 WSDL 文件并修改了我的服务以从项目的资源中读取。

@WebServiceClient(name = "MyService", targetNamespace = "http://www.myservice.com/MyService", wsdlLocation = "/documentation/wsdl/MyService.wsdl")
public class MyService extends Service {

    private final static URL MYSERVICE_WSDL_LOCATION;
    private final static WebServiceException MYSERVICE_EXCEPTION;
    private final static QName MYSERVICE_QNAME = new QName("http://www.myservice.com/MyService", "MyService");

    static {
        URL url = null;
        WebServiceException e = null;
        try {
            url = URL.class.getResource("/documentation/wsdl/MyService.wsdl");
        } catch (Exception ex) {
            e = new WebServiceException(ex);
        }
        MYSERVICE_WSDL_LOCATION = url;
        MYSERVICE_EXCEPTION = e;
    }
  ...
}

在本地运行,它完美运行。在 AWS Lambda 上运行,出现以下错误:

FATAL Failed to access the WSDL at: file:/documentation/wsdl/MyService.wsdl. It failed with: 
    /documentation/wsdl/MyService.wsdl (No such file or directory).
> javax.xml.ws.WebServiceException: Failed to access the WSDL at: file:/documentation/wsdl/MyService.wsdl. It failed with: 
    /documentation/wsdl/MyService.wsdl (No such file or directory).
    at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.tryWithMex(RuntimeWSDLParser.java:250)

我错过了什么?

标签: java-8aws-lambdawsdl

解决方案


从@WebServiceClient 中删除“wsdllocation”属性并更改加载资源方法可以解决问题:

@WebServiceClient(name = "MyService", targetNamespace = "http://www.myservice.com/MyService")
public class MyService extends Service {

    private final static URL MYSERVICE_WSDL_LOCATION;
    private final static WebServiceException MYSERVICE_EXCEPTION;
    private final static QName MYSERVICE_QNAME = new QName("http://www.myservice.com/MyService", "MyService");

    static {
        URL url = null;
        WebServiceException e = null;
        try {
            url = Thread.currentThread().getContextClassLoader().getResource("documentation/wsdl/MyService.wsdl");
        } catch (Exception ex) {
            e = new WebServiceException(ex);
        }
        MYSERVICE_WSDL_LOCATION = url;
        MYSERVICE_EXCEPTION = e;
    }
  ...
}

推荐阅读