创建工程:
一个服务端,一个客户端
添加相关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:sgorWS_Clientsrcmainjava 执行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("======================================================"); } } }
运行结果:
项目目录: