CXF2.7整合spring发布webservice,返回值类型是Map和List<Map>类型
在昨天研究了发布CXF发布webservice之后想着将以前的项目发布webservice接口,可是怎么也发布不起来,服务启动失败,原来是自己的接口有返回值类型是Map。
研究了一番之后,发现:
webService可以处理Java 数据类型、JavaBean、List等,但是却不能处理Map数据类型,也不能返回List<Map>类型数据。于是做了两个研究:
1.发布返回值类型是Map<String,Object>类型接口
在发布的时候竟然神奇般的发布成功了,接口如下:
接口:
package cn.qlq.service; import java.sql.SQLException; import java.util.Map; import javax.jws.WebMethod; import javax.jws.WebService; import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; import cn.qlq.adapter.MapAdapter; import cn.qlq.domain.User; @WebService public interface UserService { /** * 保存用户 * * @param user * @return * @throws SQLException */ public int saveUser(User user) throws SQLException; /** * 根据userId获取user * * @param userId * @return */ @WebMethod public User getUserById(int userId); /** * 根据userId获取userMap * * @param userId * @return */ public Map<String,Object> getUserMapById(int id); }
实现类:
package cn.qlq.service.impl; import java.sql.SQLException; import java.util.List; import java.util.Map; import javax.jws.WebService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import cn.qlq.dao.UserDao; import cn.qlq.domain.User; import cn.qlq.service.UserService; @Service @WebService(targetNamespace = "http://service.qlq.cn") public class UserServiceImpl implements UserService { @Autowired private UserDao userDao; @Override public int saveUser(User user) throws SQLException { System.out.println("----------------保存user----------"); return 0; } @Override public User getUserById(int userId) { System.out.println("----------------获取user----------" + userId); return userDao.getUserById(userId); } @Override public Map<String, Object> getUserMapById(int id) { return userDao.getUserMapById(id); } }
发布webservice还是上一篇的方式,启动成功之后采用动态代理方式访问:
import javax.xml.namespace.QName; import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory; public class TestWS { public static void main(String[] args) throws Exception { JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance(); org.apache.cxf.endpoint.Client client = dcf.createClient("http://localhost/CXFTest/WS/userws?wsdl"); // url为调用webService的wsdl地址 QName name = new QName("http://service.qlq.cn/", "getUserMapById");// namespace是命名空间,methodName是方法名 Object[] objects; try { objects = client.invoke(name,5);// 第一个参数是上面的QName,第二个开始为参数,可变数组 System.out.println(objects[0].toString()); } catch (Exception e) { e.printStackTrace(); } } }
结果是正常的,如下:(还是map类型)
cn.qlq.service.GetUserMapByIdResponse$Return@273aa934
如果报错,我们可以加一个转换器,如下:
package cn.qlq.service; import java.sql.SQLException; import java.util.Map; import javax.jws.WebMethod; import javax.jws.WebService; import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; import cn.qlq.adapter.MapAdapter; import cn.qlq.domain.User; @WebService public interface UserService { /** * 保存用户 * * @param user * @return * @throws SQLException */ public int saveUser(User user) throws SQLException; /** * 根据userId获取user * * @param userId * @return */ @WebMethod public User getUserById(int userId); /** * 根据userId获取userMap * * @param userId * @return */ @XmlJavaTypeAdapter(MapAdapter.class) public Map<String,Object> getUserMapById(int id); }
编写转换器:
这里参数需要一个实现了XmlAdapter类的适配器类;
这里的话XmlAdapter要加两个参数,XmlAdapter<ValueType,BoundType>
ValueType是cxf能接收的类型,BoundType是cxf不能接受的类型;
package cn.qlq.adapter; import java.util.HashMap; import java.util.Map; import javax.xml.bind.annotation.adapters.XmlAdapter; import cn.qlq.domain.User; public class MapAdapter extends XmlAdapter<User,Map<String, Object>> { @Override public Map<String, Object> unmarshal(User v) throws Exception { Map result = new HashMap(); result.put("username", v.getUsername()); result.put("password", v.getPassword()); return result; } @Override public User marshal(Map<String, Object> v) throws Exception { User u = new User(); u.setUsername((String) v.get("username")); u.setPassword((String) v.get("password")); return u; } }
测试代码还是上面代码,结果:(被转成User对象)
cn.qlq.service.User@530db33e
另外附一通用的map转换器:
package cn.qlq.adapter; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; import javax.xml.bind.annotation.adapters.XmlAdapter; public class MapAdapter extends XmlAdapter<MapEntity[], Map> { @Override public MapEntity[] marshal(Map map) throws Exception { // TODO Auto-generated method stub MapEntity[] list = new MapEntity[map.size()]; Set keyset = map.keySet(); int index =0; for(Iterator itor=keyset.iterator();itor.hasNext();){ MapEntity item = new MapEntity(); item.key = itor.next(); item.value = map.get(item.key); list[index++] = item; } return list; } @Override public Map unmarshal(MapEntity[] list) throws Exception { // TODO Auto-generated method stub Map map = new HashMap(); for(int i=0;i<list.length;i++){ MapEntity item = list[i]; map.put(item.key, item.value); } return map; } } class MapEntity{ public Object key; public Object value; }
2.发布返回值类型是List<Map<String,Object>>类型接口
在上面service增加一个接口:
package cn.qlq.service; import java.sql.SQLException; import java.util.List; import java.util.Map; import javax.jws.WebMethod; import javax.jws.WebService; import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; import cn.qlq.adapter.MapAdapter; import cn.qlq.adapter.UserMapAdapter; import cn.qlq.domain.User; @WebService public interface UserService { /** * 保存用户 * * @param user * @return * @throws SQLException */ public int saveUser(User user) throws SQLException; /** * 根据userId获取user * * @param userId * @return */ @WebMethod public User getUserById(int userId); /** * 模拟获取所有的用户,一个map代表一个用户 * * @return */public List<Map<String, Object>> getAllUsers(); /** * 根据userId获取userMap * * @param userId * @return */ @XmlJavaTypeAdapter(MapAdapter.class) public Map<String, Object> getUserMapById(int id); }
实现类:
package cn.qlq.service.impl; import java.sql.SQLException; import java.util.List; import java.util.Map; import javax.jws.WebService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import cn.qlq.dao.UserDao; import cn.qlq.domain.User; import cn.qlq.service.UserService; @Service @WebService(targetNamespace = "http://service.qlq.cn") public class UserServiceImpl implements UserService { @Autowired private UserDao userDao; @Override public int saveUser(User user) throws SQLException { System.out.println("----------------保存user----------"); return 0; } @Override public User getUserById(int userId) { System.out.println("----------------获取user----------" + userId); return userDao.getUserById(userId); } @Override public List<Map<String, Object>> getAllUsers() { return userDao.getAllUsers(); } @Override public Map<String, Object> getUserMapById(int id) { return userDao.getUserMapById(id); } }
直接发布报错如下:
严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userWS': Invocation of init method failed; nested exception is javax.xml.ws.WebServiceException: org.apache.cxf.service.factory.ServiceConstructionException at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1422) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:518) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:455) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:293) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:290) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:192) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425) at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:282) at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:204) at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47) at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5118) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5634) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1571) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1561) at java.util.concurrent.FutureTask.run(FutureTask.java:262) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) at java.lang.Thread.run(Thread.java:745) Caused by: javax.xml.ws.WebServiceException: org.apache.cxf.service.factory.ServiceConstructionException at org.apache.cxf.jaxws.EndpointImpl.doPublish(EndpointImpl.java:369) at org.apache.cxf.jaxws.EndpointImpl.publish(EndpointImpl.java:251) at org.apache.cxf.jaxws.EndpointImpl.publish(EndpointImpl.java:537) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeCustomInitMethod(AbstractAutowireCapableBeanFactory.java:1546) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1487) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1419) ... 21 more Caused by: org.apache.cxf.service.factory.ServiceConstructionException at org.apache.cxf.jaxb.JAXBDataBinding.initialize(JAXBDataBinding.java:332) at org.apache.cxf.service.factory.AbstractServiceFactoryBean.initializeDataBindings(AbstractServiceFactoryBean.java:86) at org.apache.cxf.service.factory.ReflectionServiceFactoryBean.buildServiceFromClass(ReflectionServiceFactoryBean.java:478) at org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean.buildServiceFromClass(JaxWsServiceFactoryBean.java:690) at org.apache.cxf.service.factory.ReflectionServiceFactoryBean.initializeServiceModel(ReflectionServiceFactoryBean.java:540) at org.apache.cxf.service.factory.ReflectionServiceFactoryBean.create(ReflectionServiceFactoryBean.java:252) at org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean.create(JaxWsServiceFactoryBean.java:205) at org.apache.cxf.frontend.AbstractWSDLBasedEndpointFactory.createEndpoint(AbstractWSDLBasedEndpointFactory.java:102) at org.apache.cxf.frontend.ServerFactoryBean.create(ServerFactoryBean.java:159) at org.apache.cxf.jaxws.JaxWsServerFactoryBean.create(JaxWsServerFactoryBean.java:211) at org.apache.cxf.jaxws.EndpointImpl.getServer(EndpointImpl.java:454) at org.apache.cxf.jaxws.EndpointImpl.doPublish(EndpointImpl.java:334) ... 30 more Caused by: com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions java.util.Map is an interface, and JAXB can't handle interfaces. this problem is related to the following location: at java.util.Map at private java.util.Map cn.qlq.service.jaxws_asm.GetAllUsersResponse._return at cn.qlq.service.jaxws_asm.GetAllUsersResponse at com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException$Builder.check(IllegalAnnotationsException.java:91) at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(JAXBContextImpl.java:442) at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:274) at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:125) at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1127) at com.sun.xml.internal.bind.v2.ContextFactory.createContext(ContextFactory.java:130) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:248) at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:235) at javax.xml.bind.ContextFinder.find(ContextFinder.java:445) at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:637) at org.apache.cxf.common.jaxb.JAXBContextCache.createContext(JAXBContextCache.java:278) at org.apache.cxf.common.jaxb.JAXBContextCache.getCachedContextAndSchemas(JAXBContextCache.java:172) at org.apache.cxf.jaxb.JAXBDataBinding.createJAXBContextAndSchemas(JAXBDataBinding.java:464) at org.apache.cxf.jaxb.JAXBDataBinding.initialize(JAXBDataBinding.java:330) ... 41 more
解决办法也是编写转换器并且在接口的抽象方法上声明转换器或者用一个bean接收Map中的entry
(1)第一种解决办法:不知道什么原因我直接写转换器转List<Map>老是报错,最后没办法了就将List<Map>放进map中再进行转换器转换,如下:(需要转换器)
dao层模拟从数据看查询的直接是List<Map>
@Override public List<Map<String, Object>> getAllUsers() { //模拟从数据库取数据 List<Map<String, Object>> result = new ArrayList<Map<String, Object>>(); for(int i=0;i<3;i++){ Map map = new HashMap(); map.put("username", i); map.put("password", i); result.add((HashMap<String, Object>) map); } return result; }
service接口方法:
@XmlJavaTypeAdapter(MapAdapter3.class) public Map<String,List<Map<String, Object>>> getAllUsers();
service实现类实现方法:(就是将List<Map>又装到一个Map中了)
@Override public Map<String, List<Map<String, Object>>> getAllUsers() { Map result = new HashMap(); List<Map<String, Object>> allUsers = userDao.getAllUsers(); result.put("allUsers", allUsers); return result; }
转换器:
package cn.qlq.adapter; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.xml.bind.annotation.adapters.XmlAdapter; import cn.qlq.domain.User; @SuppressWarnings("all") public class MapAdapter3 extends XmlAdapter<User[], Map<String, List<Map<String, Object>>>> { @Override public Map<String, List<Map<String, Object>>> unmarshal(User[] v) throws Exception { Map result = new HashMap(); List<Map> list = new ArrayList(); for (User u : v) { Map map = new HashMap(); map.put("username", u.getUsername()); map.put("password", u.getPassword()); list.add(map); } result.put("allUsers", list); return result; } @Override public User[] marshal(Map<String, List<Map<String, Object>>> v) throws Exception { int length = v.size(); User u[] = new User[length]; List<Map<String, Object>> userMap = v.get("allUsers"); for (int i = 0; i < length; i++) { Map m = userMap.get(i); User user = new User(); user.setPassword(m.get("username").toString()); user.setPassword(m.get("password").toString()); u[i] = user; } return u; } }
测试代码:
import javax.xml.namespace.QName; import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory; public class TestWS { public static void main(String[] args) throws Exception { JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance(); org.apache.cxf.endpoint.Client client = dcf.createClient("http://localhost/CXFTest/WS/userws?wsdl"); // url为调用webService的wsdl地址 QName name = new QName("http://service.qlq.cn/", "getAllUsers");// namespace是命名空间,methodName是方法名 Object[] objects; try { objects = client.invoke(name);// 第一个参数是上面的QName,第二个开始为参数,可变数组 System.out.println(objects[0].toString()); } catch (Exception e) { e.printStackTrace(); } } }
结果:
cn.qlq.service.UserArray@7b17b8ad
(2)第二种方法我们可以编写一个实体类接收Map,也就是map中的一个entry对应bean的一个属性,这样我们只用拿List<BeanName>替代List<Map>(不需要转换器====推荐这种方式)
dao层还是上面的代码:
@Override public List<Map<String, Object>> getAllUsers() { //模拟从数据库取数据 List<Map<String, Object>> result = new ArrayList<Map<String, Object>>(); for(int i=0;i<3;i++){ Map map = new HashMap(); map.put("username", i); map.put("password", i); result.add((HashMap<String, Object>) map); } return result; }
比如上面每个map都有username和password,我们可以写一个实体:
package cn.qlq.domain; public class User { private String username; private String password; 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; } @Override public String toString() { return "User [username=" + username + ", password=" + password + "]"; } }
service方法:
/** * 模拟获取所有的用户,一个map代表一个用户 * * @return */ public List<User> getAllUsers();
serviceImpl实现方法
@Override public List<User> getAllUsers() { List<Map<String, Object>> allUsers = userDao.getAllUsers(); List<User> users = new ArrayList(); for (Map userMap : allUsers) { User u = new User(); u.setUsername(userMap.get("username").toString()); u.setPassword(userMap.get("password").toString()); users.add(u); } return users; }
测试代码:
import javax.xml.namespace.QName; import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory; public class TestWS { public static void main(String[] args) throws Exception { JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance(); org.apache.cxf.endpoint.Client client = dcf.createClient("http://localhost/CXFTest/WS/userws?wsdl"); // url为调用webService的wsdl地址 QName name = new QName("http://service.qlq.cn/", "getAllUsers");// namespace是命名空间,methodName是方法名 Object[] objects; try { objects = client.invoke(name);// 第一个参数是上面的QName,第二个开始为参数,可变数组 System.out.println(objects[0].toString()); } catch (Exception e) { e.printStackTrace(); } } }
结果:
[cn.qlq.service.User@1a89b61f, cn.qlq.service.User@4713bad5, cn.qlq.service.User@604f1a67]