处理JavaBean
服务端
- 创建实体类User
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
public class Role {
private Integer id; // 编号
private String roleName; // 角色名称
public Role() {
super();
// TODO Auto-generated constructor stub
}
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;
}
}
- 在服务接口中添加getRoleByUser(User user) 方法,并在实现类中实现该方法
@WebService
public interface HelloWorld {
public String say(String str);
public List<Role> getRoleByUser(User user);
}
@WebService
public class HelloWorldImpl implements HelloWorld{
public String say(String str) {
return "Hello "+str;
}
public List<Role> getRoleByUser(User user) {
List<Role> roleList=new ArrayList<Role>();
// 模拟 直接写死
if(user!=null){
if(user.getUserName().equals("java1234") && user.getPassword().equals("123456")){
roleList.add(new Role(1,"技术总监"));
roleList.add(new Role(2,"架构师"));
}else if(user.getUserName().equals("jack") && user.getPassword().equals("123456")){
roleList.add(new Role(3,"程序员"));
}
return roleList;
}else{
return null;
}
}
}
wsdl2java生成客户端代码
客户端
- 在Client主类中,传递一个User,作为服务端的方法参数
public class Client {
public static void main(String[] args) {
HelloWorldService service=new HelloWorldService();
HelloWorld helloWorld=service.getHelloWorldPort();
//System.out.println(helloWorld.say("java1234"));
User user=new User();
user.setUserName("jack");
user.setPassword("123456");
List<Role> roleList=helloWorld.getRoleByUser(user);
for(Role role:roleList){
System.out.println(role.getId()+","+role.getRoleName());
}
}
}
处理Map类型
前面讲的一些都是简单类型,cxf都支持。但是有些复杂类型,cxf是不支持,比如常用的Map类型;
服务端
- 服务接口方法及其实现类:
/**
* 获取所有用户以及对应的角色
* @return
*/
public Map<String,List<Role>> getRoles();
public Map<String, List<Role>> getRoles() {
Map<String,List<Role>> map=new HashMap<String,List<Role>>();
List<Role> roleList1=new ArrayList<Role>();
roleList1.add(new Role(1,"技术总监"));
roleList1.add(new Role(2,"架构师"));
map.put("java1234", roleList1);
List<Role> roleList2=new ArrayList<Role>();
roleList2.add(new Role(1,"程序员"));
map.put("jack", roleList2);
return map;
}
- 如果服务端代码写成这样,启动Server类,会报错。解决方法是使用@XmlJavaTypeAdapter注解,加载接口定义上。
package com.java1234.webservice;
import java.util.List;
import java.util.Map;
import javax.jws.WebService;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import com.java1234.adapter.MapAdapter;
import com.java1234.entity.Role;
import com.java1234.entity.User;
@WebService
public interface HelloWorld {
public String say(String str);
public List<Role> getRoleByUser(User user);
/**
* 获取所有用户以及对应的角色
* @return
*/
@XmlJavaTypeAdapter(MapAdapter.class)
public Map<String,List<Role>> getRoles();
}
- 适配器类 XmlAdapter
package com.java1234.adapter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import com.java1234.entity.Role;
/**
* Map适配器
* @author Administrator
*
*/
public class MapAdapter extends XmlAdapter<MyRole[], Map<String,List<Role>>>{
/**
* 适配转换 MyRole[] -> Map<String, List<Role>>
*/
@Override
public Map<String, List<Role>> unmarshal(MyRole[] v) throws Exception {
Map<String, List<Role>> map=new HashMap<String,List<Role>>();
for(int i=0;i<v.length;i++){
MyRole r=v[i];
map.put(r.getKey(), r.getValue());
}
return map;
}
/**
* 适配转换 Map<String, List<Role>> -> MyRole[]
*/
@Override
public MyRole[] marshal(Map<String, List<Role>> 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;
}
}