大家都知道jdk1.6及以后都支持了對webService的原生態的支持;它在發布時會生成一個wsdl和一個xsd(一個類只生成一個xsd)所以就保留了引用關系,如下:
<?xml version="1.0" encoding="UTF-8" ?> - <!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6. --> - <!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6. --> - <definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://jaxws.zzz/jaxws/hello" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://jaxws.zzz/jaxws/hello" name="HelloService"> - <types> - <xsd:schema> <xsd:import namespace="http://jaxws.zzz/jaxws/hello" schemaLocation="http://localhost:8080/services/HelloService?xsd=1" /> </xsd:schema> </types> - <message name="sayHello"> <part name="parameters" element="tns:sayHello" /> </message> - <message name="sayHelloResponse"> <part name="parameters" element="tns:sayHelloResponse" /> </message> - <portType name="HelloService"> - <operation name="sayHello"> <input message="tns:sayHello" /> <output message="tns:sayHelloResponse" /> </operation> </portType> - <binding name="HelloServicePortBinding" type="tns:HelloService"> <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" /> - <operation name="sayHello"> <soap:operation soapAction="" /> - <input> <soap:body use="literal" /> </input> - <output> <soap:body use="literal" /> </output> </operation> </binding> - <service name="HelloService"> - <port name="HelloServicePort" binding="tns:HelloServicePortBinding"> <soap:address location="http://localhost:8080/services/HelloService" /> </port> </service> </definitions>
但是由於osb平台目前不能識別這種引用關系,即所有引用都應該在一個文件中可見;為了解決這個問題:可以采用的方法就是手動把生成的這兩個文件(wsdl和xsd文件)手動合並成一個文件比如取名"HelloService.wsdl”;把文件放到classpath路徑下,或者發布webService需要的地方,然后在發布webService之前,修改webservice的定義;退如下:
@WebService(serviceName = "HelloService", portName = "HelloServicePort", targetNamespace = "http://jaxws.zzz/jaxws/hello",wsdlLocation="HelloService.wsdl") @SOAPBinding(style = SOAPBinding.Style.DOCUMENT, use = SOAPBinding.Use.LITERAL, parameterStyle = SOAPBinding.ParameterStyle.WRAPPED) public class HelloService { @WebMethod public String sayHello(String s) { System.out.println("hello," + s); return s; } }
添加“wsdlLocation="HelloService.wsdl" ”;這個就可以了;
得到結果將是:
<?xml version="1.0" encoding="UTF-8" ?> - <!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6. --> - <!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6. --> - <!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6. --> - <definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://jaxws.zzz/jaxws/hello" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://jaxws.zzz/jaxws/hello" name="HelloService"> - <types> - <xs:schema xmlns:tns="http://jaxws.zzz/jaxws/hello" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0" targetNamespace="http://jaxws.zzz/jaxws/hello"> <xs:element name="sayHello" type="tns:sayHello" /> <xs:element name="sayHelloResponse" type="tns:sayHelloResponse" /> - <xs:complexType name="sayHello"> - <xs:sequence> <xs:element name="arg0" type="xs:string" minOccurs="0" /> </xs:sequence> </xs:complexType> - <xs:complexType name="sayHelloResponse"> - <xs:sequence> <xs:element name="return" type="xs:string" minOccurs="0" /> </xs:sequence> </xs:complexType> </xs:schema> </types> - <message name="sayHello"> <part name="parameters" element="tns:sayHello" /> </message> - <message name="sayHelloResponse"> <part name="parameters" element="tns:sayHelloResponse" /> </message> - <portType name="HelloService"> - <operation name="sayHello"> <input message="tns:sayHello" /> <output message="tns:sayHelloResponse" /> </operation> </portType> - <binding name="HelloServicePortBinding" type="tns:HelloService"> <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" /> - <operation name="sayHello"> <soap:operation soapAction="" /> - <input> <soap:body use="literal" /> </input> - <output> <soap:body use="literal" /> </output> </operation> </binding> - <service name="HelloService"> - <port name="HelloServicePort" binding="tns:HelloServicePortBinding"> <soap:address location="http://localhost:8080/services/HelloService" /> </port> </service> </definitions>
對於另一種import問題就是當我們定義一個接口一個實現類的時候,如果兩個接口和實現類不一個package中時,也會出現對wsdl的引用問題,此時只需要在實現類的annotation中添加endpointInterface="com.zzz.jaxws.service.HelloService"(即接口類包路徑名);
參考:http://stackoverflow.com/questions/16030574/jax-ws-has-xsd-schema-in-different-url(不過我不得不聲名其實拼wsdl文件這個方法我也想到了,只是還沒有來得及去試試就先看了這個文章,嘿嘿)