創建工程:
一個服務端,一個客戶端

添加相關jar
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sgor.com</groupId>
<artifactId>WC_Server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-core</artifactId>
<version>3.1.6</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.1.6</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>3.1.6</version>
</dependency>
</dependencies>
</project>
編寫接口類
package com.sgor.webservice;
import java.util.List;
import java.util.Map;
import javax.jws.WebService;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import com.sgor.entity.Role;
import com.sgor.entity.User;
import com.sgor.util.adapter.MapAdapter;
@WebService
public interface WS_Interface {
public String say(String str);
public List<Role> getUserRole(User user);//登陸驗證 模擬
@XmlJavaTypeAdapter(MapAdapter.class)
public Map<String,List<Role>> getRoles();//不支持類型適配器
}
接口實現
package com.sgor.webservice.impl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.jws.WebService;
import com.sgor.entity.Role;
import com.sgor.entity.User;
import com.sgor.webservice.WS_Interface;
@WebService
public class WS_InterfaceImpl implements WS_Interface {
public List<Role> getUserRole(User user) {
List<Role> list = new ArrayList<Role>();
try {
if (!user.equals("") || user != null) {
if (user.getAccount().equals("admin")
&& user.getPassword().equals("123456")) {
list.clear();
list.add(new Role(1, "系統管理員"));
list.add(new Role(2, "系統操作員"));
} else if (user.getAccount().equals("pd")
&& user.getPassword().equals("123456")) {
list.clear();
list.add(new Role(3, "程序員"));
} else {
list.clear();
list.add(new Role(9999, "賬號不存在或密碼錯誤!"));
}
}
} catch (Exception e) {
list.add(new Role(9997, "系統異常,暫時無法完成請求"));
return list;
}
return list;
}
public Map<String, List<Role>> getRoles() {
Map<String, List<Role>> map =new HashMap<String, List<Role>>();
List<Role> list = new ArrayList<Role>();
list.add(new Role(1,"系統管理員"));
list.add(new Role(2,"系統操作員"));
map.put("admin", list);
List<Role> list1 = new ArrayList<Role>();
list1.add(new Role(1,"系統設計師"));
list1.add(new Role(2,"系統開發者"));
map.put("pd", list1);
return map;
}
}
適配器
package com.sgor.util.adapter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import com.sgor.entity.Role;
import com.sgor.util.entity.MapRole;
public class MapAdapter extends XmlAdapter<MapRole[], Map<String,List<Role>>>{
@Override
public Map<String, List<Role>> unmarshal(MapRole[] v) throws Exception {
// TODO Auto-generated method stub
Map<String, List<Role>> map = new HashMap<String, List<Role>>();
for(int i=0;i<v.length;i++){
MapRole maprole = v[i];
map.put(maprole.getKey(), maprole.getValue());
}
return map;
}
@Override
public MapRole[] marshal(Map<String, List<Role>> v) throws Exception {
MapRole[] roles = new MapRole[v.size()];
int i=0;
for(String key:v.keySet()){
//聲明roles[i]
roles[i] = new MapRole();
roles[i].setKey(key);
roles[i].setValue(v.get(key));
i++;
}
return roles;
}
}
entity
package com.sgor.util.entity;
import java.util.List;
import com.sgor.entity.Role;
public class MapRole {
private String key;
private List<Role> value;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public List<Role> getValue() {
return value;
}
public void setValue(List<Role> value) {
this.value = value;
}
}
package com.sgor.entity;
public class Role {
private int id;//編號
private String rolename;//角色
public Role() {
super();
}
public Role(int id, String rolename) {
super();
this.id = id;
this.rolename = rolename;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getRolename() {
return rolename;
}
public void setRolename(String rolename) {
this.rolename = rolename;
}
}
package com.sgor.entity;
public class User {
private int id;//編號
private String account;//登陸名
private String name;//用戶名
private String password;//密碼
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
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;
}
}
Server端
package com.sgor.server;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
import com.sgor.webservice.WS_Interface;
import com.sgor.webservice.impl.WS_InterfaceImpl;
public class WS_Server {
public static void main(String[] args) {
System.out.println("WebService 啟動開始");
WS_Interface implementor = new WS_InterfaceImpl();
String address="http://127.0.0.1/Ws_Service";//接口地址設置
//方式一:使用jdk自帶方法創建服務端url
//Endpoint.publish(address, implementor);//jdk自帶創建webservice接口
//方式二:使用cxf創建服務端url
JaxWsServerFactoryBean factoryBean = new JaxWsServerFactoryBean();
factoryBean.setAddress(address);//cxf設置地址
factoryBean.setServiceClass(WS_Interface.class);//設置接口類
factoryBean.setServiceBean(implementor);//設置接口實現類
factoryBean.create();//創建接口
System.out.println("WebService 啟動成功");
}
}
啟動服務

瀏覽器輸入剛剛創建的url http://127.0.0.1/Ws_Service?wsdl

使用工具解析xml生成代碼,去官網下載工具包之后配置環境變量
xxx:/apache-cxf-xxx/bin;配置到path中即可

配置完之后輸入wsdl2java確認配置成功

創建client工程 去相應的目錄下執行wsdl2java http://127.0.0.1/Ws_Service?wsdl


創建客戶端:
package com.sgor.Client;
import java.util.List;
import com.sgor.webservice.MapRole;
import com.sgor.webservice.MapRoleArray;
import com.sgor.webservice.Role;
import com.sgor.webservice.User;
import com.sgor.webservice.WSInterface;
import com.sgor.webservice.WSInterfaceService;
public class WS_Client {
public static void main(String[] args) {
WSInterfaceService wsinterfaceservice = new WSInterfaceService();
WSInterface wsinterface = wsinterfaceservice.getWSInterfacePort();
//向服務器端發送參數 請求返回
/**
* 使用cxf工具包生成解析類步驟:
* 1.下載對應版本工具包 如apache-cxf-3.1.10
* 2.配置環境變量在path中添加xxx://apache-cxf-3.1.10/bin
* 3.執行批處理文件CMD>>進入需要工具類目錄如E:\sgor\WS_Client\src\main\java 執行wsdl2java 服務端url
* 4.刷新工程文件夾 創建客戶端調用方法
*/
User user = new User();
user.setId(1);
user.setAccount("admin");
user.setPassword("123456");
List<Role> rolelist = wsinterface.getUserRole(user);
System.out.println("*****************getUserRole()方法測試*****************");
System.out.println("賬號:"+user.getAccount()+"密碼:"+user.getPassword());
System.out.println("系統查詢結果如下:");
for(Role list:rolelist){
System.out.println(list.getId()+" "+list.getRolename());
}
System.out.println("******************getRoles()方法測試*******************");
MapRoleArray array=wsinterface.getRoles();
List<MapRole> roleList=array.getItem();
for (int i = 0; i < roleList.size(); i++) {
MapRole roles=roleList.get(i);
System.out.println("分組:"+roles.getKey());
for(Role r:roles.getValue()){
System.out.println("編號:"+r.getId()+" 角色:"+r.getRolename());
}
System.out.println("======================================================");
}
}
}
運行結果:

項目目錄:


