先拋出這一章的結論: 1.當返回值,形參的類型是String,基本類型時,cxf能夠輕鬆處理 2.當返回值,形參的類型是javaBean複合類,List集合,數組等,cxf能夠也輕鬆處理 3.Map,非javaBean的複合類,cxf是處理不了的,後面會將到
接着上一張直接上代碼
java
==================================服務端===========================web
//服務端接口代碼 /** * @author xp * @Title: WebServiceDemo.java * @Package com.xp.cn * @Description: TODO * @date 2016年4月30日 下午8:27:44 * @version V1.0 */ package com.xp.cn.ws; import java.util.List; import javax.jws.WebService; import com.xp.cn.bean.Cat; import com.xp.cn.bean.User; /** * @author xp * @ClassName: WebServiceDemo * @Description: webService服務端 * @date 2016年4月30日 下午8:27:44 */ @WebService public interface IWebServiceDemo { String sayHello(String name); List<Cat> getCatsByUser(User user); }
//服務端接口代碼實現類 /** @author xp @Title: WebServiceImpl.java @Package com.xp.cn @Description: TODO @date 2016年4月30日 下午8:39:22 @version V1.0 **/ package com.xp.cn.ws.impl; import java.util.Date; import java.util.List; import javax.jws.WebService; import com.xp.cn.bean.Cat; import com.xp.cn.bean.User; import com.xp.cn.service.IuserService; import com.xp.cn.service.impl.UserServiceImpl; import com.xp.cn.ws.IWebServiceDemo; /** @author xp @ClassName: WebServiceImpl @Description: TODO @date 2016年4月30日 下午8:39:22 **/ @WebService(endpointInterface = "com.xp.cn.ws.IWebServiceDemo", serviceName = "webServiceImpl") public class WebServiceImpl implements IWebServiceDemo { /** * 處理簡單類型 * 1.形參和返回值都是簡單類型 */ @Override public String sayHello(String name) { return "你好" + name + new Date(); } /** * 處理複雜類型 * 2.形參和返回值都是複雜類型 */ @Override public List<Cat> getCatsByUser(User user) { //webService本身並不會去實現業務邏輯功能 //webService只是調用業務邏輯主鍵 IuserService userService = new UserServiceImpl(); return userService.getCatsByUser(user); } }
//服務單javaBean,User /** * @author xp * @Title: User.java * @Package com.xp.cn.bean * @Description: TODO * @date 2016年5月1日 下午12:07:58 * @version V1.0 */ package com.xp.cn.bean; /** * @author xp * @ClassName: User * @Description: TODO * @date 2016年5月1日 下午12:07:58 * */ public class User { private Integer id; private String name; private String password; private String address; public User() { super(); } public User(Integer id, String name, String password, String address) { super(); this.id = id; this.name = name; this.password = password; this.address = address; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } /** * 只要name和password相等 * 咱們就認爲該對象是同一個對象 */ @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((name == null) ? 0 : name.hashCode()); result = prime * result + ((password == null) ? 0 : password.hashCode()); return result; } /** * 只要name和password相等 * 咱們就認爲該對象是同一個對象 */ @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; User other = (User) obj; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; if (password == null) { if (other.password != null) return false; } else if (!password.equals(other.password)) return false; return true; } }
//服務端javaBean Cat /** * @author xp * @Title: Cat.java * @Package com.xp.cn.bean * @Description: TODO * @date 2016年5月1日 下午12:11:02 * @version V1.0 */ package com.xp.cn.bean; /** * @author xp * @ClassName: Cat * @Description: TODO * @date 2016年5月1日 下午12:11:02 * */ public class Cat { private Integer id; private String name; private String color; public Cat() { super(); } public Cat(Integer id, String name, String color) { super(); this.id = id; this.name = name; this.color = color; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } }
//Service 接口組件 /** * @author xp * @Title: IuserService.java * @Package com.xp.cn.Service * @Description: TODO * @date 2016年5月1日 下午12:21:48 * @version V1.0 */ package com.xp.cn.service; import java.util.List; import com.xp.cn.bean.Cat; import com.xp.cn.bean.User; /** * @author xp * @ClassName: IuserService * @Description: TODO * @date 2016年5月1日 下午12:21:48 * */ public interface IuserService { List<Cat> getCatsByUser(User user); }
//serviceimpl /** * @author xp * @Title: UserServiceImpl.java * @Package com.xp.cn.Service * @Description: TODO * @date 2016年5月1日 下午12:22:21 * @version V1.0 */ package com.xp.cn.service.impl; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import com.xp.cn.bean.Cat; import com.xp.cn.bean.User; import com.xp.cn.service.IuserService; /** * @author xp * @ClassName: UserServiceImpl * @Description: TODO * @date 2016年5月1日 下午12:22:21 * */ public class UserServiceImpl implements IuserService { //模擬內存數據庫 static Map<User, List<Cat>> catDb = new HashMap<User, List<Cat>>(); static{ List<Cat> catList = new ArrayList<Cat>(); catList.add(new Cat(1, "cat1", "black")); catList.add(new Cat(2, "cat2", "write")); catDb.put(new User(1, "jaychou", "jaychou", "taiwan"), catList); List<Cat> catList2 = new ArrayList<Cat>(); catList2.add(new Cat(1, "cat3", "black")); catList2.add(new Cat(2, "cat3", "write")); catDb.put(new User(2, "xp", "xp", "shanghai"), catList2); } @Override public List<Cat> getCatsByUser(User user) { return catDb.get(user); } }
//發佈服務 /** * @author xp * @Title: WebServicePublish.java * @Package com.xp.cn * @Description: TODO * @date 2016年4月30日 下午9:06:49 * @version V1.0 */ package com.xp.cn; import javax.xml.ws.Endpoint; import com.xp.cn.ws.IWebServiceDemo; import com.xp.cn.ws.impl.WebServiceImpl; /** * @author xp * @ClassName: WebServicePublish * @Description: TODO * @date 2016年4月30日 下午9:06:49 * */ public class WebServicePublish { public static void main(String[] args) { IWebServiceDemo demo = new WebServiceImpl(); //調用EndPoint發佈服務 Endpoint.publish("http://127.0.0.1/XXX", demo); } }
好了,如今能夠測試是否發佈成功數據庫
打開瀏覽器地址欄輸入:http://127.0.0.1/XXX apache
出現下圖說明發布成功數組
==================================客戶端===========================瀏覽器
首先利用wsdl2java工具生成客戶端代碼ide
步驟章節和WebService簡單開發 apache-cxf-3.1.6環境配置章節同樣工具
調用webservice服務
測試
/** * @author xp * @Title: ClientMain.java * @Package com.xp.cn.test * @Description: TODO * @date 2016年5月1日 下午12:39:03 * @version V1.0 */ package com.xp.cn.test; import java.util.List; import com.xp.cn.ws.Cat; import com.xp.cn.ws.IWebServiceDemo; import com.xp.cn.ws.User; import com.xp.cn.ws.impl.WebServiceImpl; /** * @author xp * @ClassName: ClientMain * @Description: TODO * @date 2016年5月1日 下午12:39:03 * */ public class ClientMain { public static void main(String[] args) { //繼承Service的類當成工廠使用 WebServiceImpl webServiceImpl = new WebServiceImpl(); //此處返回的只是遠程webservice的代理 IWebServiceDemo webService = webServiceImpl.getWebServiceImplPort(); String sayHello = webService.sayHello("xp"); System.out.println(sayHello); User user = new User(); user.setName("jaychou"); user.setPassword("jaychou"); List<Cat> catsByUser = webService.getCatsByUser(user); for (Cat cat : catsByUser) { System.out.println(cat.getName()); System.out.println(cat.getColor()); } } }
客戶端和服務端包結構this
控制檯打印信息以下
OK!