工欲善其事,必先利其器。在開始CXF Web Service 開始之前,先進性CXF的下載,配置,我比較崇拜官網,所以給出的下載地址既是官網地址,下載地址:
http://cxf.apache.org/download.html。
接下來在Eclipse中配置CXF ,配製方法:
Windows——>preferences——>Web Services——>CXF 2.x Preferences——>Runtime下添加你的cxf的路徑,有雨我已經配置過,所以配置後結果如圖:
完成CXF的配置後,開始一個簡單的Web Service開發。
1.創建一個簡單的web項目,並將中的jar包導入項目。可以找到你的cxf-2.6.1文件下lib文件中的jar包把需要的jar複製粘貼到你項目的lib下,所需jar如圖所示:
項目右鍵——>Build Path——>Configure Build Path...——>Libraries——>Add Libraries——>CXF Runtime.
項目結構如圖:
實體類如下
import java.io.Serializable;
public class User implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private String id;
private String name;
private String age;
private String description;
public User() {
super();
}
public User(String id, String name, String age, String description) {
super();
this.id = id;
this.name = name;
this.age = age;
this.description = description;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getAge() {
return age;
}
public String getDescription() {
return description;
}
public void setId(String id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setAge(String age) {
this.age = age;
}
public void setDescription(String description) {
this.description = description;
}
}
創建Web Service對外暴漏的接口
import java.util.List;
import javax.jws.WebParam;
import javax.jws.WebService;
import com.lvdun.model.User;
/*
*
*
* 無論是xml + tomact
*
* 還是發佈方式,都需要
*
* @WebService
*
* */
@WebService(targetNamespace="http://com.lvdun.service/")
public interface HelloWorld {
String sayHi(@WebParam(name="text")String text);
String sayHiToUser(User user);
String[] SayHiToUserList(List<User> userList);
}
創建服務接口的實現
import java.util.List;
import javax.jws.WebParam;
import javax.jws.WebService;
import com.lvdun.model.User;
/*
*
*
* 無論是xml + tomact
*
* 還是發佈方式,都需要
*
* @WebService
*
* */
@WebService(targetNamespace="http://com.lvdun.service/")
public interface HelloWorld {
String sayHi(@WebParam(name="text")String text);
String sayHiToUser(User user);
String[] SayHiToUserList(List<User> userList);
}
服務端的啓動
import javax.xml.ws.Endpoint;
import com.lvdun.serviceImpl.HelloWorldImpl;
/*
*
* http://blog.csdn.net/mafan121/article/details/43271153
*
*
* http://blog.csdn.net/hu_shengyang/article/details/38384597
*
* */
public class WebServiceApp {
public static void main(String[] args) {
System.out.println("web service start");
HelloWorldImpl implementor = new HelloWorldImpl();
String address = "http://localhost:8080/WebService/services/WebService";
Endpoint.publish(address, implementor);
System.out.println("web service started");
}
}
<span style="font-size:24px;">http://localhost:8080/WebService/services/WebService</span><span style="font-size: 24px; font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">?wsdl,若看到如下效果則說明該WebService發佈成功。</span>
<span style="font-size: 24px; font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"></span><p><span style="font-size: 24px;">客戶端訪問</span></p><p><span style="font-size: 24px;">
</span></p><p><span style="font-size: 24px;">新建客戶端項目,java web或者java項目都可以,導入的jar包如下</span></p><p><span style="font-size: 24px;">
</span></p><p><span style="font-size: 24px;">客戶端項目結構如圖</span></p><p><span style="font-size: 24px;">
</span></p><p><span style="font-size: 24px;">客戶端啓動如下</span></p><p></p><pre code_snippet_id="1903566" snippet_file_name="blog_20160927_6_1171960" name="code" class="java">import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import com.lvdun.model.User;
import com.lvdun.service.HelloWorld;
public class WebServiceClient {
public static void main(String[] args) {
JaxWsProxyFactoryBean jwpfb = new JaxWsProxyFactoryBean();
jwpfb.setServiceClass(HelloWorld.class);
jwpfb.setAddress("http://localhost:8080/WebService/services/WebService");
HelloWorld hw = (HelloWorld) jwpfb.create();
User user = new User();
user.setName("馬克思");
user.setDescription("懷念馬克思");
System.out.println(hw.sayHiToUser(user)+"=======");
}
}
</pre><pre code_snippet_id="1903566" snippet_file_name="blog_20160927_8_15718" name="code" class="java">以上服務器端是通過 Endpoint.publish(address, implementor);發佈的,若不喜歡這種方式,也可以
部署到tomact中,在tomact中部署需要進行一下配置
首先在web-inf下新建cxf-servlet.xml,如下:
<pre name="code" class="html"><?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"
xmlns:soap="http://cxf.apache.org/bindings/soap"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<!-- ;服務接口 -->
<!-- address爲服務發佈二級地址 完整地址爲 /項目發佈名稱/cfx攔截地址/address (cfx攔截地址在web.xml中url-pattern標籤中配置) -->
<!--服務實現類 -->
<jaxws:server id="jaxwsService" serviceClass="com.lvdun.service.HelloWorld" address="/WebService" >
<jaxws:serviceBean >
<bean class="com.lvdun.serviceImpl.HelloWorldImpl" />
</jaxws:serviceBean>
</jaxws:server>
</beans>
</pre>還需要在web.xml中配置如下<p></p><pre>
<pre name="code" class="html"><?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>WebService</display-name>
<servlet>
<servlet-name>cxf</servlet-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>60</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
</pre><pre code_snippet_id="1903566" snippet_file_name="blog_20160927_15_8117153" name="code" class="html">客戶端的請求方式也包括很多種,<pre name="code" class="java">import java.lang.reflect.Method;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
public class JaxWsDynamicClient {
public static void main(String[] args) throws Exception {
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient("http://localhost:8080/WebService/services/WebService?wsdl");
//這種方式調用model要和service接口放在一個package中
Object user = Thread.currentThread().getContextClassLoader().loadClass("com.lvdun.service.User").newInstance();
// Object user = Class.forName("com.lvdun.model.User").newInstance();
Method m1 = user.getClass().getMethod("setName", String.class);
Method m2 = user.getClass().getMethod("setDescription", String.class);
m1.invoke(user, "馬克思");
m2.invoke(user, "懷念馬克思");
Object[] response = client.invoke("sayHi", "ceshi");
Object[] response1 = client.invoke("sayHiToUser", user);
System.out.println("Response is " + response[0]);
System.out.println("Response is " + response1[0]);
}
}
不過這種請求方式需要修改自己Eclipse的編譯方式是jdk而不是jre,我就是在這上面浪費了
很多時間,還有其他請求方式如,ajax等等,這些我還沒有做出來,就不放代
碼了。
這是源代碼地址 :<a target=_blank href="http://download.csdn.net/detail/u012577528/9641484">CXF Web Service & client </a></span>
可能有些人已經發現我的demo和網上的很多例子相同,但是網上的例子很多
關鍵點沒有講到,會給新學者造成一定的困惑,我就是踩了很多坑,才把網上
的例子跑出來。
由於我的表述能力較弱,可能很多知識都沒有體現,希望學習的朋友多多思考
如各個註解是什麼意思。
其中我主要參考的博客已經在代碼註釋中體現,現在我再把參考的博客放在最
後,供大家參考:
<a target=_blank href="http://blog.csdn.net/mafan121/article/details/43271153">WebService——CXF方式創建WebService服務端 </a>
<a target=_blank href="http://blog.csdn.net/hu_shengyang/article/details/38384597">CXF實現webService服務(一) </a>
<a target=_blank href="http://blog.csdn.net/look85927/article/details/13000117"> webservice之CXF註解實現(一) </a>
<a target=_blank href="http://reymont.iteye.com/blog/1511176">開發CXF JAVA客戶端</a>
遇到問題不要着急,多思考,喜歡看stack overflow。
</pre><pre code_snippet_id="1903566" snippet_file_name="blog_20160927_38_8136081" name="code" class="java">