CXF处理JavaBean、Map以及复合类型
程序员文章站
2022-04-02 16:15:50
...
1. CXF处理JavaBean以及复合类型
2. CXF处理一些Map等复杂类型
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 List<Role> getRoleByUser(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<Role> getRoleByUser(User user) { List<Role> roleList = new ArrayList<Role>(); 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"); List<Role> roleList = 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 List<Role> getRoleByUser(User user); @XmlJavaTypeAdapter(MapAdapter.class) public Map<String,List<Role>> 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<Role> getRoleByUser(User user) { List<Role> roleList = new ArrayList<Role>(); 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<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("andrew", roleList1); List<Role> roleList2 = new ArrayList<Role>(); 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<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; } @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; } } MyRole.java package com.andrew.adapter; import java.util.List; import com.andrew.entity.Role; public class MyRole { 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; } }
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"); List<Role> roleList = helloWorld.getRoleByUser(user); for (Role role : roleList) { System.out.println(role.getId() + "," + role.getRoleName()); } MyRoleArray array = helloWorld.getRoles(); List<MyRole> 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(); } } }
下一篇: CXF拦截器和自定义拦截器