论坛首页 Java企业应用论坛

调用webservice中含返回值为自定义对象的方法时,报ClassCastException错误

浏览 4734 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2014-11-01   最后修改:2014-11-01

本人初学webservice,平台用spring+cxf+ibatis,在调用接口中含返回自定义对象的方法时,就会报ClassCastException,即方法:

public List<User> getUserList()

调用返回值为基本类型的方法,则没问题,如:

public Integer getUserCount();

困扰很久,包括调用参数中含自定义类型的方法,也会报同样错误,如:

public void bImportData(List<User> uList);

请教各位大牛,盼回复,谢啦~\(≧▽≦)/~

 

1、服务端Webservice接口

@WebService
public interface ICxfDemo {
       /**
	 * 获取用户数量
	 * @return
	 */
	@WebMethod(operationName = "getUserCount", action = "urn:GetUserCount")
	public Integer getUserCount();


	/**
	 * 获取用户列表
	 * @return
	 */
	@WebMethod(operationName = "getUserList", action = "urn:GetUserList")
	public List<User> getUserList();
}

 

2、服务端webservice接口实现类:

@WebService(endpointInterface = "com.ocrlp.serv.ws.ICxfDemo",serviceName = "CxfDemoImpl") 
public class CxfDemoImpl implements ICxfDemo {
	
        private ServiceManager servMgr;
	@Resource(name="servMgr") 
	public void setServMgr(ServiceManager servMgr) {
		this.servMgr = servMgr;
	}
	
       /**
	 * 获取用户数量
	 * @return
	 */
	@Override
	public Integer getUserCount() {
		return servMgr.getUserRowCount();
	}

	/**
	 * 获取用户列表
	 * @return
	 */
	@Override
	public List<User> getUserList() {
		return servMgr.listAll_Users();
	}
}

 

3、beans.xml

    <jaxws:endpoint id="cxfDemo" 
        implementor="com.ocrlp.serv.ws.CxfDemoImpl" address="/cxfDemo" />

 
4、客户端java调用测试代码:

public class ICxfDemoTest {
	
	private ICxfDemoTest() {}
	private static ICxfDemo dsServ;
	
	/**
	 * 初始化构造方法
	 * @return
	 */
	synchronized public static ICxfDemo init() {
		if (dsServ == null) {
			JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
			factory.setServiceClass(ICxfDemo.class); // 注册WebService接口
			factory.setAddress("http://127.0.0.1/web/webservice/cxfDemo"); // 设置WebService地址
			dsServ = (ICxfDemo)factory.create(); 
		}
		return dsServ;
	}

	public Integer getUserCount() {
		return dsServ.getUserCount();
	}
	
	public static void main(String[] args) {
		System.out.println("客户端调用结果:" + ICxfDemoTest.init().getUserCount());
	}
}

 

5、实体类是可序列化的

public class User implements Serializable {
	
	private static final long serialVersionUID = 2519452507329194492L;
	private String uid;
	private String username;
	private String password;
	private String realname;
	private String org;
	private String job;

	//getter , setter 省略

}

 

6、运行eclipse服务器测试报错:

2014-11-1 22:20:32 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://ws.terminal.ocrlinepatrol.com/}ICxfDemoService from class com.ocrlinepatrol.terminal.ws.ICxfDemo
Exception in thread "main" java.lang.ClassCastException: com.sun.xml.bind.v2.model.impl.RuntimeBuiltinLeafInfoImpl$5 cannot be cast to com.sun.xml.bind.v2.model.impl.ClassInfoImpl
    at com.sun.xml.bind.v2.model.impl.ClassInfoImpl.getBaseClass(ClassInfoImpl.java:171)
    at com.sun.xml.bind.v2.model.impl.RuntimeClassInfoImpl.getBaseClass(RuntimeClassInfoImpl.java:61)
    at com.sun.xml.bind.v2.model.impl.RuntimeClassInfoImpl.getBaseClass(RuntimeClassInfoImpl.java:41)
    at com.sun.xml.bind.v2.model.impl.ModelBuilder.getClassInfo(ModelBuilder.java:149)
    at com.sun.xml.bind.v2.model.impl.RuntimeModelBuilder.getClassInfo(RuntimeModelBuilder.java:55)
    at com.sun.xml.bind.v2.model.impl.RuntimeModelBuilder.getClassInfo(RuntimeModelBuilder.java:41)
    at com.sun.xml.bind.v2.model.impl.ModelBuilder.getTypeInfo(ModelBuilder.java:196)
    at com.sun.xml.bind.v2.model.impl.TypeRefImpl.calcRef(TypeRefImpl.java:56)
    at com.sun.xml.bind.v2.model.impl.TypeRefImpl.getTarget(TypeRefImpl.java:33)
    at com.sun.xml.bind.v2.model.impl.RuntimeTypeRefImpl.getTarget(RuntimeTypeRefImpl.java:22)
    at com.sun.xml.bind.v2.model.impl.RuntimeTypeRefImpl.getTarget(RuntimeTypeRefImpl.java:15)
    at com.sun.xml.bind.v2.model.impl.ElementPropertyInfoImpl$1.get(ElementPropertyInfoImpl.java:38)
    at com.sun.xml.bind.v2.model.impl.ElementPropertyInfoImpl$1.get(ElementPropertyInfoImpl.java:41)
    at java.util.AbstractList$Itr.next(AbstractList.java:345)
    at com.sun.xml.bind.v2.model.impl.ModelBuilder.getClassInfo(ModelBuilder.java:146)
    at com.sun.xml.bind.v2.model.impl.RuntimeModelBuilder.getClassInfo(RuntimeModelBuilder.java:55)
    at com.sun.xml.bind.v2.model.impl.RuntimeModelBuilder.getClassInfo(RuntimeModelBuilder.java:41)
    at com.sun.xml.bind.v2.model.impl.ModelBuilder.getTypeInfo(ModelBuilder.java:196)
    at com.sun.xml.bind.v2.model.impl.ModelBuilder.getTypeInfo(ModelBuilder.java:211)
    at com.sun.xml.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(JAXBContextImpl.java:383)
    at com.sun.xml.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:236)
    at com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:76)
    at com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:55)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:202)
    at javax.xml.bind.ContextFinder.find(ContextFinder.java:363)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:574)
    at org.apache.cxf.jaxb.JAXBDataBinding.createJAXBContext(JAXBDataBinding.java:427)
    at org.apache.cxf.jaxb.JAXBDataBinding.initialize(JAXBDataBinding.java:221)
    at org.apache.cxf.service.factory.ReflectionServiceFactoryBean.buildServiceFromClass(ReflectionServiceFactoryBean.java:313)
    at org.apache.cxf.service.factory.ReflectionServiceFactoryBean.initializeServiceModel(ReflectionServiceFactoryBean.java:362)
    at org.apache.cxf.service.factory.ReflectionServiceFactoryBean.create(ReflectionServiceFactoryBean.java:156)
    at org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean.create(JaxWsServiceFactoryBean.java:89)
    at org.apache.cxf.frontend.AbstractWSDLBasedEndpointFactory.createEndpoint(AbstractWSDLBasedEndpointFactory.java:74)
    at org.apache.cxf.frontend.ClientFactoryBean.create(ClientFactoryBean.java:51)
    at org.apache.cxf.frontend.ClientProxyFactoryBean.create(ClientProxyFactoryBean.java:92)
    at com.ocrlinepatrol.terminal.ws.ICxfDemoTest.init(ICxfDemoTest.java:34)
    at com.ocrlinepatrol.terminal.ws.ICxfDemoTest.main(ICxfDemoTest.java:64)

 

   发表时间:2014-11-05  

光序列化传递对象是不够的,需要做出声明,这点在所有歌webservice框架中都是一样的。 cxf应该像这样:

package com.ws.cxf.pojo;

import java.io.Serializable;

import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

@WebService
@SOAPBinding(style = Style.RPC)
public class User implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4490740575622507301L;

	private String userName;
	private int age;

	public User(){};
	public User(String userName, int age) {
		this.userName = userName;
		this.age = age;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return userName + " is " + age + " years olds";
	};

}

 

 

0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics