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; } }
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; } }
@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; } } }
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()); } } }
前面講的一些都是簡單類型,cxf都支持。可是有些複雜類型,cxf是不支持,好比經常使用的Map類型;java
/** * 獲取全部用戶以及對應的角色 * @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; }
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(); }
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; } }