2012-06-13 38 views
7

我正在開發與JAX-WS的WebService(我在jaxws-maven-plugin上使用wsimport目標)。我編寫了一個導入XSD模式的WSDL。xsd架構不提供wsdl

WEB-INF/wsdl/service.wsdl 
WEB-INF/wsdl/service.xsd 

此外我生成的Web服務類和創建端點和所有。到目前爲止,一切都很好。當我在Tomcat 7上運行我的服務時,一切都很好。我可以從我的瀏覽器訪問wsdl:

http://localhost:8080/webService/servlet-url?wsdl 

但我無法訪問xsd模式。問題是,在這個WSDL:

<xsd:schema> 
<xsd:import namespace="http://ws.service/domain/1.0" schemaLocation="service.xsd"/> 
</xsd:schema> 

當然代班WSDL和XSD的過程是本地路徑上,但我希望他們的網絡服務正在運行時被遠程訪問。我知道schemaLocation應該是這樣的「http:// localhost:8080/webService/servlet-url?xsd = 1」。

在WSDL在瀏覽器中輸入呈現schould樣子:

<xsd:schema> 
    <xsd:import namespace="http://ws.service/domain/1.0" schemaLocation="http://localhost:8080/webService/servlet-url?wsdl&resource=service.xsd"/> 
    </xsd:schema> 

本地主機:8080/web服務/ servlet的WSDL給我:

wsdl:definitions targetNamespace="http://ws.serv.com/Service/1.0" name="emuiaService">   
<wsdl:types> 
    <xsd:schema> 
     <xsd:import namespace="http://ws.serv.com/Service/domain/1.0" schemaLocation="schema.xsd"/> 
    </xsd:schema> 
</wsdl:types> 
<wsdl:message name="halloMsg"> 
    <wsdl:part name="parameters" element="dom:halloRequest"/> 
</wsdl:message> 
<wsdl:message name="halloResponseMsg"> 
    <wsdl:part name="return" element="dom:halloResponse"/> 
</wsdl:message> 

等等...

回答

4

我幾乎不敢相信這是一個很難解決的問題!

我一直在瘋狂地谷歌搜索這個問題的解決方案!然後,我一直在努力尋找自己的解決方案。通過調試器遍歷java-6-openjdk的默認javax.xml.ws.spi.Provider實現(JRE中的「工廠」,它創建用於發佈Web服務的javax.xml.ws.Endpoint對象)I終於學到了一些東西,這讓我手藝是在Java SE至少工作,至少在我目前的JRE,這是一個解決方案:

java version "1.6.0_33" 
OpenJDK Runtime Environment (IcedTea6 1.13.5) (6b33-1.13.5-1ubuntu0.12.04) 
OpenJDK Server VM (build 23.25-b01, mixed mode) 

無論這種解決方案是Java EE使用我不知道然而。

這是我如何解決它:

package myservice; 

import java.io.IOException; 
import java.io.InputStream; 
import java.net.URL; 
import java.util.Arrays; 
import javax.xml.transform.Source; 
import javax.xml.transform.stream.StreamSource; 
import javax.xml.ws.Endpoint; 

public class App 
{ 
    private static final String MY_SERVICE_XSD = "/wsdl/MyService.xsd"; 

    public static void main(String[] args) 
    { 
     Endpoint ep = Endpoint.create(new MyEndpointImpl()); 

     ep.setMetadata(Arrays.asList(sourceFromResource(MY_SERVICE_XSD))); 

     ep.publish("http://localhost:8080/svc/hello"); 
    } 

    private static Source sourceFromResource(String name) { 
     URL resource = App.class.getResource(name); 
     String systemId = resource.toExternalForm(); 
     InputStream inputStream; 
     try { 
      inputStream = resource.openStream(); 
     } catch (IOException e) { 
      throw new RuntimeException("Failed to create InputStream from resource \""+ name +"\"", e); 
     } 
     return new StreamSource(inputStream, systemId); 
    } 
} 

關鍵的東西是我第一次使用方法端點#創建(不端點#發佈),以獲得一個未發表端點。然後,我將XSD文件作爲「元數據」添加到(仍未發佈的)端點(代碼「ep.setMetaData(...)」)。 然後我發佈端點(代碼「ep.publish(...)」)。

現在,當我訪問http://localhost:8080/svc/hello?wsdl我得到:

<definitions targetNamespace="http://somewhere.net/my/namespace" name="MyService"> 
     <types> 
      <xsd:schema> 
       <xsd:import namespace="http://somewhere.net/my/namespace" 
          schemaLocation="http://localhost:8080/svc/hello?xsd=1"/> 
      </xsd:schema> 
     </types> 
        ... 
    </definitions> 

和我的XSD文件可從http://localhost:8080/svc/hello?xsd=1

注意,磁盤上的我的MyService.wsdl文件仍包含:

  <xsd:schema> 
       <xsd:import namespace="http://somewhere.net/my/namespace" 
          schemaLocation="MyService.xsd"></xsd:import> 
      </xsd:schema> 
+0

我真的不記得我做了什麼...可能切換到最後合同,但感謝您解決它。我希望有一天能幫助別人;) – bemol

0

好的,我們走吧。

進入WSDL文件modificate像這樣

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<wsdl:definitions 
     targetNamespace="http://service.wsr.company.com/" 
     name="webServiceExample" 
     xmlns="http://schemas.xmlsoap.org/wsdl/" 
     xmlns:tns="http://servicio.wsr.baz.com/" 
     xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
     xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" 
     xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"> 

在這個小片段重要的包括xmlns標籤。那些用於部署模式XSD。接下來

<wsdl:types> 
    <xs:schema 
     xmlns:tns="http://service.wsr.company.com/" 
     xmlns:xs="http://www.w3.org/2001/XMLSchema" 
     targetNamespace="http://service.wsr.company.com/" version="1.0"> 

     ... 

    </xs:schema> 
</wsdl:types> 

到下面你的標籤會得到什麼,你有service.xsd文件或證明它http://localhost:8080/webService/servlet-url?xsd=1我們繼續

<wsdl:message name="your_method_name"> 
     <wsdl:part name="parameters" element="tns:your_method_name"/> 
    </wsdl:message> 
    <wsdl:message name="your_method_nameResponse"> 
     <wsdl:part name="parameters" element="tns:your_method_nameResponse"/> 
    </wsdl:message> 

上面的標籤都顯示你的方法名。 Next

<wsdl:portType name="webServiceExample"> 
      <wsdl:operation name="your_method_name"> 
      <wsdl:input message="tns:your_method_name"/> 
       <wsdl:output message="tns:your_method_nameResponse"/> 
      </wsdl:operation> 
    </wsdl:portType> 

那些以上tar是用於放置您的操作的。繼續

<wsdl:binding name="webServiceExamplePortBinding" type="tns:webServiceExample"> 
     <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/> 
     <wsdl:operation name="your_method_name"> 
      <soap:operation soapAction=""/> 
      <wsdl:input> 
       <soap:body use="literal"/> 
      </wsdl:input> 
      <wsdl:output> 
       <soap:body use="literal"/> 
      </wsdl:output> 
     </wsdl:operation> 
    </wsdl:binding> 

下一個:)

<wsdl:service name="webServiceExample"> 
    <wsdl:port name="webServiceExamplePort" binding="tns:webServiceExamplePortBinding"> 
     <soap:address location="REPLACE_WITH_ACTUAL_URL"/> 
</wsdl:port> 

最後完成:)

注意,您必須通過標籤來改變當前標籤<wsdl:...></wsdl:...>

保存它,公衆和你玩得開心XSD架構在WSDL中提供。

我希望能幫到你。再見。

+0

不幸的是,沒有工作。靜態模式xsd不通過http顯示。 – bemol

+0

請告訴我你是如何做的並向我展示你的WSDL文件。 :) – hekomobile