【问题标题】:how to get rid of WSDL dependency after generating client code using that WSDL?使用该 WSDL 生成客户端代码后如何摆脱 WSDL 依赖?
【发布时间】:2016-02-02 14:38:50
【问题描述】:

我使用 wsimport 从一个 WSDL 文件生成客户端代码,我已经设法测试了这个生成的代码并且它可以工作,但是有一个问题,代码引用了这样的 WSDL 文件,

static {
    URL url = null;
    try {
        URL baseUrl;
        baseUrl = com.spx.global.spf_wmis_dev.etq2sapintegration_zqm246f_etq_validate_matnr_webservice.matnr_provider.ETQ2SAPIntegrationZQM246FETQVALIDATEMATNRWebServiceMATNRProvider.class.getResource(".");
        url = new URL(baseUrl, "file:/C:/Users/xx/Desktop/spx/wsdls/validate_MATNR.wsdl");
    } catch (MalformedURLException e) {
        logger.warning("Failed to create URL for the wsdl Location: 'file:/C:/Users/xx/Desktop/spx/wsdls/validate_MATNR.wsdl', retrying as a local file");
        logger.warning(e.getMessage());
    }
    ETQ2SAPINTEGRATIONZQM246FETQVALIDATEMATNRWEBSERVICEMATNRPROVIDER_WSDL_LOCATION = url;
}

我已经使用在http://localhost:8088/validate 上运行的 soapUI 为该 WSDL 文件创建了一个模拟服务,如果我像这样在 WSDL 文件中使用这个确切的 URL,一切都会正常工作。

<wsdl:port name="ETQ2SAPIntegration_ZQM246F_ETQ_VALIDATE_MATNR_WebService_MATNR_provider_Port" binding="tns:ETQ2SAPIntegration_ZQM246F_ETQ_VALIDATE_MATNR_WebService_MATNR_provider_Binder">
  <soap:address location="http://localhost:8088/validate"/>
</wsdl:port>

现在我希望能够在不参考 WSDL 文件的情况下指定 Web 服务位置,我尝试了很多方法,例如覆盖端口 endPointAddress,

ETQ2SAPIntegrationZQM246FETQVALIDATEMATNRWebServiceMATNRProvider ser= new ETQ2SAPIntegrationZQM246FETQVALIDATEMATNRWebServiceMATNRProvider(); this class extends the Service class
        javax.xml.namespace.QName qn= new QName("http://SPF-WMIS-DEV.GLOBAL.SPX.COM/ETQ2SAPIntegration.ZQM246F_ETQ_VALIDATE_MATNR.WebService:MATNR_providera", "ETQ2SAPIntegration.ZQM246F_ETQ_VALIDATE_MATNR.WebService.MATNR_provider");
        ser.addPort(qn,HTTPBinding.HTTP_BINDING, "http://localhost:8088/test");
        ZQM246FETQVALIDATEMATNRResponse response = new ZQM246FETQVALIDATEMATNRResponse();
        MATNRProviderPortType provider = ser.getETQ2SAPIntegrationZQM246FETQVALIDATEMATNRWebServiceMATNRProviderPort();

但是无论我尝试什么代码都只是不断尝试从 WSDL 文件中读取 endPointAddress,也许我正在尝试做的事情是不可行的,这意味着我不能只是摆脱 WSDL 文件,我不确定在这一点上,所以任何建议都会很有帮助。

P.S:不要怪我没有遵守 Java 命名约定,我和你一样讨厌它。

【问题讨论】:

  • 如果您需要更多解释,请询问资源。

标签: java web-services soap wsdl


【解决方案1】:

您可以像这样更改请求上下文(为了更好的可读性,我缩短了服务和提供者的名称):

String address = "http://localhost:8088/validate";
BlaBlaWebService service = new BlaBlaWebService();
BlaBlaProviderPort provider = ser.getBlaBlaProviderPort();
if (provider instanceof BindingProvider) {
    BindingProvider bindProvider = (BindingProvider)provider;
    bindProvider.getRequestContext().put(
        BindingProvider.ENDPOINT_ADDRESS_PROPERTY, address);
} else {
   //log or fail if you want
}

【讨论】:

  • 过去 8 小时左右我一直在寻找解决方案,非常感谢 :))
  • 如果你能回答我,再问一个问题,为什么生成的代码在运行时需要 WSDL 文件?
  • @QuakeCore 这里有一个很好的答案:stackoverflow.com/questions/20101009/…
  • 信不信由你我偶然发现了这个问题,并且与您之前的回答类似,我想我只是无法建立正确的联系,但现在一切都清楚了,再次感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-03-14
  • 1970-01-01
  • 2012-06-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-20
相关资源
最近更新 更多