JAX-WS客户端:访问本地WSDL的正确路径是什么?
问题是我需要从提供的文件中构建Web服务客户端。我已将此文件存储在本地文件系统上,并且在将WSDL文件保留在正确的文件系统文件夹中的同时,一切都很好。当我将其部署到服务器上或从文件系统文件夹中删除WSDL时,代理找不到WSDL并引发错误。我已经在网上搜索过,但发现了以下帖子,但仍无法使它起作用:
JAX-WS从jar中加载WSDL
http://www.java.net/forum/topic/glassfish/metro -and-jaxb / client-jar-cant-
find-local-wsdl-0
http://blog.vinodsingh.com/2008/12/locally-packaged-
wsdl.html
我正在使用NetBeans 6.1(这是一个旧应用程序,必须使用此新的Web服务客户端进行更新)。下面是JAX-WS代理类:
@WebServiceClient(name = "SOAService", targetNamespace = "http://soaservice.eci.ibm.com/", wsdlLocation = "file:/C:/local/path/to/wsdl/SOAService.wsdl")public class SOAService
extends Service
{
private final static URL SOASERVICE_WSDL_LOCATION;
private final static Logger logger = Logger.getLogger(com.ibm.eci.soaservice.SOAService.class.getName());
static {
URL url = null;
try {
URL baseUrl;
baseUrl = com.ibm.eci.soaservice.SOAService.class.getResource(".");
url = new URL(baseUrl, "file:/C:/local/path/to/wsdl/SOAService.wsdl");
} catch (MalformedURLException e) {
logger.warning("Failed to create URL for the wsdl Location: 'file:/C:/local/path/to/wsdl/SOAService.wsdl', retrying as a local file");
logger.warning(e.getMessage());
}
SOASERVICE_WSDL_LOCATION = url;
}
public SOAService(URL wsdlLocation, QName serviceName) {
super(wsdlLocation, serviceName);
}
public SOAService() {
super(SOASERVICE_WSDL_LOCATION, new QName("http://soaservice.eci.ibm.com/", "SOAService"));
}
/**
* @return
* returns SOAServiceSoap
*/
@WebEndpoint(name = "SOAServiceSOAP")
public SOAServiceSoap getSOAServiceSOAP() {
return super.getPort(new QName("http://soaservice.eci.ibm.com/", "SOAServiceSOAP"), SOAServiceSoap.class);
}
/**
* @param features
* A list of {@link javax.xml.ws.WebServiceFeature} to configure on the proxy. Supported features not in the <code>features</code> parameter will have their default values.
* @return
* returns SOAServiceSoap
*/
@WebEndpoint(name = "SOAServiceSOAP")
public SOAServiceSoap getSOAServiceSOAP(WebServiceFeature... features) {
return super.getPort(new QName("http://soaservice.eci.ibm.com/", "SOAServiceSOAP"), SOAServiceSoap.class, features);
}
}
这是我使用代理的代码:
WebServiceClient annotation = SOAService.class.getAnnotation(WebServiceClient.class); // trying to replicate proxy settings
URL baseUrl = com.ibm.eci.soaservice.SOAService.class.getResource("");//note : proxy uses "."
URL url = new URL(baseUrl, "/WEB-INF/wsdl/client/SOAService.wsdl");
//URL wsdlUrl = this.getClass().getResource("/META-INF/wsdl/SOAService.wsdl");
SOAService serviceObj = new SOAService(url, new QName(annotation.targetNamespace(), annotation.name()));
proxy = serviceObj.getSOAServiceSOAP();
/* baseUrl;
//classes\com\ibm\eci\soaservice
//URL url = new URL(baseUrl, "../../../../wsdl/SOAService.wsdl");
proxy = new SOAService().getSOAServiceSOAP();*/
//updating service endpoint
Map<String, Object> ctxt = ((BindingProvider)proxy ).getRequestContext();
ctxt.put(JAXWSProperties.HTTP_CLIENT_STREAMING_CHUNK_SIZE, 8192);
ctxt.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, WebServiceUrl);
NetBeans将WSDL的副本放在 web-inf / wsdl / client / SOAService中 ,所以我也不想将其添加到 META-
INF中 。服务类位于 WEB-INF / classes / com / ibm / eci / soaservice /中
,baseurl变量包含它的文件系统完整路径(c:\ path \ to \ the \ project … \
soaservice)。上面的代码引发错误:
javax.xml.ws.WebServiceException:无法在以下位置访问WSDL:文件:/WEB-
INF/wsdl/client/SOAService.wsdl。失败并显示:\ WEB-INF \ wsdl \ client \
SOAService.wsdl(找不到路径)
因此,首先,我是否应该更新代理类的wsdllocation?然后,如何告诉WEB-INF / classes / com / ibm / eci /
soaservice中的SOAService类在\ WEB-INF \ wsdl \ client \ SOAService.wsdl中搜索WSDL?
:我找到了另一个链接 : **//jianmingli.com/wp/?cat** =
41,它表示将WSDL放入类路径中。我很ask愧地问:如何将其放入Web应用程序的类路径中?
回答:
最好的选择是使用jax-ws-catalog.xml
编译本地WSDL文件时,请覆盖WSDL位置并将其设置为类似
http://localhost/wsdl/SOAService.wsdl
不用担心,这只是一个URI而不是URL,这意味着您不必在该地址处使用WSDL。
您可以通过将wsdllocation选项传递给wsdl到Java编译器来实现。
这样做将从以下位置更改您的代理代码
static { URL url = null;
try {
URL baseUrl;
baseUrl = com.ibm.eci.soaservice.SOAService.class.getResource(".");
url = new URL(baseUrl, "file:/C:/local/path/to/wsdl/SOAService.wsdl");
} catch (MalformedURLException e) {
logger.warning("Failed to create URL for the wsdl Location: 'file:/C:/local/path/to/wsdl/SOAService.wsdl', retrying as a local file");
logger.warning(e.getMessage());
}
SOASERVICE_WSDL_LOCATION = url;
}
至
static { URL url = null;
try {
URL baseUrl;
baseUrl = com.ibm.eci.soaservice.SOAService.class.getResource(".");
url = new URL(baseUrl, "http://localhost/wsdl/SOAService.wsdl");
} catch (MalformedURLException e) {
logger.warning("Failed to create URL for the wsdl Location: 'http://localhost/wsdl/SOAService.wsdl', retrying as a local file");
logger.warning(e.getMessage());
}
SOASERVICE_WSDL_LOCATION = url;
}
注意,URL构造函数中的file://更改为http://。
现在出现在jax-ws-catalog.xml中。如果没有jax-ws-catalog.xml,jax-ws确实会尝试从该位置加载WSDL。
[http://localhost/wsdl/SOAService.wsdl](http://localhost/wsdl/SOAService.wsdl)
并失败,因为没有这样的WSDL将可用。
但是使用jax-ws-catalog.xml时,只要它尝试访问WSDL @,就可以将jax-ws重定向到本地打包的WSDL。
[http://localhost/wsdl/SOAService.wsdl](http://localhost/wsdl/SOAService.wsdl)
。
这是jax-ws-catalog.xml
<catalog xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog" prefer="system"> <system systemId="http://localhost/wsdl/SOAService.wsdl"
uri="wsdl/SOAService.wsdl"/>
</catalog>
您正在执行的操作是告诉jax-ws,何时需要从以下位置加载WSDL:
[http://localhost/wsdl/SOAService.wsdl](http://localhost/wsdl/SOAService.wsdl)
,则应从本地路径wsdl / SOAService.wsdl加载它。
现在应该将wsdl / SOAService.wsdl和jax-ws-catalog.xml放在哪里?那是一百万美元的问题,不是吗?
它应该在您的应用程序jar的META-INF目录中。
所以像这样
ABCD.jar | __ META-INF
| __ jax-ws-catalog.xml
| __ wsdl
| __ SOAService.wsdl
这样,您甚至不必覆盖客户端中访问代理的URL。WSDL是从JAR内部获取的,并且避免了在代码中必须包含硬编码的文件系统路径的情况。
有关jax-ws-catalog.xml的更多信息 http://jax-ws.java.net/nonav/2.1.2m1/docs/catalog-
support.html
希望能有所帮助
以上是 JAX-WS客户端:访问本地WSDL的正确路径是什么? 的全部内容, 来源链接: utcz.com/qa/424155.html