原本是想在xfire生成的wsdl文件中添加<wsdl:documentation>,找了几天都没有找到相应的api,无意中发现了CXF,于是改用CXF。CXF的前身叫 Apache CeltiXfire,现已更名为CXF。CXF 继承了 Celtix 和 XFire 两大开源项目的精华,提供了对JAX-WS全面的支持,并且提供了多种 Binding 、DataBinding、Transport 以及各种 Format 的支持,并且可以根据实际项目的需要,采用代码优先(Code First)或者 WSDL 优先(WSDL First)来实现 Web Services 的发布和使用。
这篇文章主要是实践一下采用代码优先来发布Web Service,并在发布的wsdl中添加注释<wsdl:documentation>。因为查了很多资料,借鉴的地方有些都记不清了,希望原作者谅解,这也是我对自己学习的一个总结,希望对有遇到同样问题的同学有所帮助,理解不对的地方也请指出,多交流,共进步。言归正传。
准备工作:
- 在Apache网站上下载cfx包,我这里下载的是apache-cxf-2.6.1.zip,解压到某一路径。然后设置环境变量。这主要是为了以后可以直接在命令行内输入指令生成接口类,这里以后用到时再详述。我设置为CXF_HOME=D:\apache-cxf-2.6.1。path中加入%CXF_HOME%bin;。
- 打开MyEclipse(我的版本是8.5),把cxf的所有jar包加入自定义的用户类库里,方便之后的开发使用。Window->Preferences->Java->Build Path->User Libraries,点击new,在User library name中输入自己命名的类名,我的是apache-cxf-2.6.1,ok后,点击Add JARs,选中解压文件包lib包里的全部jar文件,确定。则关于cxf的jar文件包导入完成。如图显示。
下面开始利用代码发布web服务。
1. 新建一个webservice项目。File->new->Web Service Project。我的命名为WSTest。
2..将之前创建的cxf类包导入。右键项目名,Build Path->Add Library->User Library->next,选中之前创建的类,我的是叫apache-cxf-2.6.1,点击Finish。WSTest项目中多出一个名为apache-cxf-2.6.1的类,导入成功。
3.在src下创建两个包,我的是demo.ws.client和demo.ws.server。
接口类 HelloWorld.java代码如下
package demo.ws.server;
import javax.jws.WebParam;
import javax.jws.WebService;
import org.apache.cxf.annotations.WSDLDocumentation;
import org.apache.cxf.annotations.WSDLDocumentationCollection;
@WebService
@WSDLDocumentationCollection(
{
@WSDLDocumentation("My portType description"),
@WSDLDocumentation(value = "My top level description",
placement = WSDLDocumentation.Placement.TOP),
@WSDLDocumentation(value = "My binding description",
placement = WSDLDocumentation.Placement.BINDING)
}
)
public interface HelloWorld {
@WSDLDocumentation( "this is an operation description")
String sayHi(@WebParam(name="text")String text);
}
实现类HelloWorldImpl.java代码如下
package demo.ws.server;
import javax.jws.WebService;
import javax.jws.*;
@WebService(
endpointInterface = "demo.ws.server.HelloWorld",
serviceName = "HelloWorld")
public class HelloWorldImpl implements HelloWorld{
public String sayHi(String text){
return "Hello" + text;
}
}
服务发布类DeployHelloWorldService.jjava代码如下。
package demo.ws.server;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;
@WebService(endpointInterface = "joe.test.cxf.HelloWorld",
serviceName = "HelloWorld")
public class DeployHelloWorldService {
public static void deployService(){
System.out.println("Start Service...");
HelloWorldImpl helloWorld = new HelloWorldImpl();
String address = "http://localhost:8080/helloworld";
Endpoint.publish(address, helloWorld);
}
public static void main(String[] args) throws InterruptedException{
deployService();
System.out.println("Start Server");
Thread.sleep(1000 * 60);
System.out.println("End Server");
System.exit(0);
}
}
修改WEB_INF下的web.xml如下
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/beans.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
在WEB_INF下添加beans.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<jaxws:endpoint id="webServiceHelloWorld"
address="/HelloWorld"
implementor="demo.ws.server.HelloWorldImpl"/>
</beans>
服务部署后,地址栏输入http://localhost:8080/WSTest/HelloWorld?wsdl,则显示如下:
<?xml version="1.0" encoding="UTF-8" ?>
- <wsdl:definitions name="HelloWorld" targetNamespace="http://server.ws.demo/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://server.ws.demo/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<wsdl:documentation>My top level description</wsdl:documentation>
- <wsdl:types>
- <xs:schema elementFormDefault="unqualified" targetNamespace="http://server.ws.demo/" version="1.0" xmlns:tns="http://server.ws.demo/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="sayHi" type="tns:sayHi" />
<xs:element name="sayHiResponse" type="tns:sayHiResponse" />
- <xs:complexType name="sayHi">
- <xs:sequence>
<xs:element minOccurs="0" name="text" type="xs:string" />
</xs:sequence>
</xs:complexType>
- <xs:complexType name="sayHiResponse">
- <xs:sequence>
<xs:element minOccurs="0" name="return" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:schema>
</wsdl:types>
- <wsdl:message name="sayHiResponse">
<wsdl:part element="tns:sayHiResponse" name="parameters" />
</wsdl:message>
- <wsdl:message name="sayHi">
<wsdl:part element="tns:sayHi" name="parameters" />
</wsdl:message>
- <wsdl:portType name="HelloWorld">
<wsdl:documentation>My portType description</wsdl:documentation>
- <wsdl:operation name="sayHi">
<wsdl:documentation>this is an operation description</wsdl:documentation>
<wsdl:input message="tns:sayHi" name="sayHi" />
<wsdl:output message="tns:sayHiResponse" name="sayHiResponse" />
</wsdl:operation>
</wsdl:portType>
- <wsdl:binding name="HelloWorldSoapBinding" type="tns:HelloWorld">
<wsdl:documentation>My binding description</wsdl:documentation>
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" />
- <wsdl:operation name="sayHi">
<soap:operation soapAction="" style="document" />
- <wsdl:input name="sayHi">
<soap:body use="literal" />
</wsdl:input>
- <wsdl:output name="sayHiResponse">
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
- <wsdl:service name="HelloWorld">
- <wsdl:port binding="tns:HelloWorldSoapBinding" name="HelloWorldImplPort">
<soap:address location="http://localhost:8080/WSTest/HelloWorld" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
服务发布成功,<wsdl:documentation>添加成功~