首页 > 编程语言 > 详细

CXF2.7整合spring发布webservice,返回值类型是Map和List<Map>类型

时间:2018-09-15 00:57:40      阅读:51      评论:0      收藏:0      [点我收藏+]

标签:col   oca   严重   jaxb   worker   并且   数据   返回   anr   

  

  在昨天研究了发布CXF发布webservice之后想着将以前的项目发布webservice接口,可是怎么也发布不起来,服务启动失败,原来是自己的接口有返回值类型是Map。

  研究了一番之后,发现:

    webService可以处理Java 数据类型、JavaBean、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

 

 

解决办法也是编写转换器并且在接口的抽象方法上声明转换器:

 

CXF2.7整合spring发布webservice,返回值类型是Map和List<Map>类型

标签:col   oca   严重   jaxb   worker   并且   数据   返回   anr   

原文:https://www.cnblogs.com/qlqwjy/p/9649615.html

(0)
(0)
   
举报
评论 一句话评论(0
0条  
登录后才能评论!
© 2014 bubuko.com 版权所有 鲁ICP备09046678号-4
打开技术之扣,分享程序人生!
             

鲁公网安备 37021202000002号