User.java package com.andrew.entity; public class User { private Integer id; private String userName; private String password; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } } Role.java package com.andrew.entity; public class Role { private Integer id; private String roleName; public Role() { super(); } public Role(Integer id, String roleName) { super(); this.id = id; this.roleName = roleName; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getRoleName() { return roleName; } public void setRoleName(String roleName) { this.roleName = roleName; } } HelloWorld.java package com.andrew.webservice; import java.util.List; import javax.jws.WebService; import com.andrew.entity.Role; import com.andrew.entity.User; @WebService public interface HelloWorld { public String say(String str); public ListgetRoleByUser(User user); } HelloWorldImpl.java package com.andrew.webservice.impl; import java.util.ArrayList; import java.util.List; import javax.jws.WebService; import com.andrew.entity.Role; import com.andrew.entity.User; import com.andrew.webservice.HelloWorld; @WebService public class HelloWorldImpl implements HelloWorld { public String say(String str) { return "Hello " + str; } public List getRoleByUser(User user) { List roleList = new ArrayList (); if (user != null) { if (user.getUserName().equals("andrew") && user.getPassword().equals("123456")) { roleList.add(new Role(1,"老板")); roleList.add(new Role(2,"经理")); } else if (user.getUserName().equals("tony") && user.getPassword().equals("123456")) { roleList.add(new Role(3,"员工")); } return roleList; } else { return null; } } }
生成需要的webservice客户端代码 生成代码的路径E:\studyWEBSERVICECXF\WS_Client\src\main\java 1) cmd 2) cd E:\studyWEBSERVICECXF\WS_Client\src\main\java 3) wsdl2java http://192.168.18.2/helloWorld?wsdl 会在WS_Client下生成代码 4) 启动WS_Server
Client.java package com.andrew.client; import java.util.List; import com.andrew.webservice.HelloWorld; import com.andrew.webservice.HelloWorldService; import com.andrew.webservice.Role; import com.andrew.webservice.User; public class Client { public static void main(String[] args) { HelloWorldService service = new HelloWorldService(); HelloWorld helloWorld = service.getHelloWorldPort(); System.out.println(helloWorld.say("andrew")); User user = new User(); user.setUserName("tony"); user.setPassword("123456"); ListroleList = helloWorld.getRoleByUser(user); for (Role role : roleList) { System.out.println(role.getId() + "," + role.getRoleName()); } } } Hello andrew 3,员工
2. CXF处理一些Map等复杂类型
HelloWorld.java package com.andrew.webservice; import java.util.List; import java.util.Map; import javax.jws.WebService; import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; import com.andrew.adapter.MapAdapter; import com.andrew.entity.Role; import com.andrew.entity.User; @WebService public interface HelloWorld { public String say(String str); public ListgetRoleByUser(User user); @XmlJavaTypeAdapter(MapAdapter.class) public Map > getRoles(); } HelloWorldImpl.java package com.andrew.webservice.impl; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.jws.WebService; import com.andrew.entity.Role; import com.andrew.entity.User; import com.andrew.webservice.HelloWorld; @WebService public class HelloWorldImpl implements HelloWorld { public String say(String str) { return "Hello " + str; } public List getRoleByUser(User user) { List roleList = new ArrayList (); if (user != null) { if (user.getUserName().equals("andrew") && user.getPassword().equals("123456")) { roleList.add(new Role(1,"老板")); roleList.add(new Role(2,"经理")); } else if (user.getUserName().equals("tony") && user.getPassword().equals("123456")) { roleList.add(new Role(3,"员工")); } return roleList; } else { return null; } } public Map > getRoles() { Map > map = new HashMap >(); List roleList1 = new ArrayList (); roleList1.add(new Role(1, "老板")); roleList1.add(new Role(2, "经理")); map.put("andrew", roleList1); List roleList2 = new ArrayList (); roleList2.add(new Role(1, "员工")); map.put("tony", roleList2); return map; } } MapAdapter.java package com.andrew.adapter; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.xml.bind.annotation.adapters.XmlAdapter; import com.andrew.entity.Role; public class MapAdapter extends XmlAdapter >> { @Override public Map > unmarshal(MyRole[] v) throws Exception { Map > map = new HashMap >(); for(int i = 0; i < v.length; i++){ MyRole r = v[i]; map.put(r.getKey(), r.getValue()); } return map; } @Override public MyRole[] marshal(Map > v) throws Exception { MyRole[] roles = new MyRole[v.size()]; int i = 0; for(String key : v.keySet()){ roles[i] = new MyRole(); roles[i].setKey(key); roles[i].setValue(v.get(key)); i++; } return roles; } } MyRole.java package com.andrew.adapter; import java.util.List; import com.andrew.entity.Role; public class MyRole { private String key; private List value; public String getKey() { return key; } public void setKey(String key) { this.key = key; } public List getValue() { return value; } public void setValue(List value) { this.value = value; } }
1) cmd 2) cd E:\studyWEBSERVICECXF\WS_Client\src\main\java 3) wsdl2java http://192.168.18.2/helloWorld?wsdl 会在WS_Client下生成代码 4) 启动WS_Server
WS_Client package com.andrew.client; import java.util.List; import com.andrew.webservice.HelloWorld; import com.andrew.webservice.HelloWorldService; import com.andrew.webservice.MyRole; import com.andrew.webservice.MyRoleArray; import com.andrew.webservice.Role; import com.andrew.webservice.User; public class Client { public static void main(String[] args) { HelloWorldService service = new HelloWorldService(); HelloWorld helloWorld = service.getHelloWorldPort(); System.out.println(helloWorld.say("andrew")); User user = new User(); user.setUserName("tony"); user.setPassword("123456"); ListroleList = helloWorld.getRoleByUser(user); for (Role role : roleList) { System.out.println(role.getId() + "," + role.getRoleName()); } MyRoleArray array = helloWorld.getRoles(); List roleList2 = array.getItem(); for(int i = 0; i < roleList2.size(); i++){ MyRole my = roleList2.get(i); System.out.print(my.getKey() + ":"); for(Role r : my.getValue()){ System.out.print(r.getId() + "," + r.getRoleName() + " "); } System.out.println(); } } }