cxf 结合 spring 发布web service时 不能实例化User对象的问题

问题描述

在service层中调用了Dao方法来进行数据库的操作 然后又重新了UserWebservice接口及其实现类 在实现类中直接调用service的方法domain中User类public abstract class User {private String userName;private String userPassword;private String mobile;private String email;private String nickname;private String region;private java.util.Date birthday ;private String telephone;private String address;private String postcode;private String signature;private String imageUrl;private int userID;private Set<Right> rightSet = new HashSet<Right>();//getter 和setter略@Overridepublic String toString() {return "User [userName=" + userName + ", userPassword=" + userPassword+ ", mobile=" + mobile + ", email=" + email + ", nickname="+ nickname + ", region=" + region + ", birthday=" + birthday+ ", telephone=" + telephone + ", address=" + address+ ", postcode=" + postcode + ", signature=" + signature+ ", imageUrl=" + imageUrl + ", userID=" + userID+ ", rightSet=" + rightSet.size() + "]";}}public class Member extends User {@Overridepublic boolean getIsAdmin() {// TODO Auto-generated method stubreturn false;}}public class Admin extends User {@Overridepublic boolean getIsAdmin() {// TODO Auto-generated method stubreturn true;}}Right类public class Right {private int rightID;private String name;private Set<User> userSet = new HashSet<User>();public String getName() {return name;}public void setName(String name) {this.name = name;}public int getRightID() {return rightID;}public void setRightID(int rightID) {this.rightID = rightID;}public Set<User> getUserSet() {return userSet;}public void setUserSet(Set<User> userSet) {this.userSet = userSet;}@Overridepublic String toString() {return "Right [rightID=" + rightID + ", name=" + name + ", userSet="+ userSet.size() + "]";}}发布成web service后 写了一个public class ClientTest {public static void main(String[] args) throws Exception { JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); factory.setServiceClass(UserWebservice.class); factory.setAddress("http://localhost:8080/TwoTablesSSH/services/UserWebService"); UserWebservice webservice = (UserWebservice) factory.create(); ]User user = webservice.queryByID(1); System.out.println(user);}}结果出错了Exception in thread "main" org.apache.cxf.binding.soap.SoapFault: Marshalling Error: A cycle is detected in the object graph. This will cause infinitely deep XML: User [userName=admin, userPassword=admin, mobile=admin, email=admin, nickname=admin, region=null, birthday=2011-11-25 17:16:37.0, telephone=null, address=null, postcode=null, signature=null, imageUrl=null, userID=1, rightSet=4] -> Right [rightID=1, name=UpdateRight, userSet=1] -> User [userName=admin, userPassword=admin, mobile=admin, email=admin, nickname=admin, region=null, birthday=2011-11-25 17:16:37.0, telephone=null, address=null, postcode=null, signature=null, imageUrl=null, userID=1, rightSet=4]at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:141)at $Proxy30.queryByID(Unknown Source)at com.tianxiao.whut.client.ClientTest.main(ClientTest.java:22)出现了循环引用对象的问题,看网上的解决办法,将@XmlTransient放在了Right类中的@XmlTransientpublic Set<User> getUserSet() {return userSet;}结果 出现了无法实例化User的问题org.apache.cxf.interceptor.Fault: Unmarshalling Error: Unable to create an instance of com.tianxiao.whut.domain.User at org.apache.cxf.jaxb.JAXBEncoderDecoder.unmarshall(JAXBEncoderDecoder.java:801)at org.apache.cxf.jaxb.JAXBEncoderDecoder.unmarshall(JAXBEncoderDecoder.java:642)at org.apache.cxf.jaxb.io.DataReaderImpl.read(DataReaderImpl.java:156)at org.apache.cxf.interceptor.DocLiteralInInterceptor.handleMessage(DocLiteralInInterceptor.java:109)at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:263)at org.apache.cxf.endpoint.ClientImpl.onMessage(ClientImpl.java:797)at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponseInternal(HTTPConduit.java:1626)at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponse(HTTPConduit.java:1493)at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.close(HTTPConduit.java:1401)at org.apache.cxf.transport.AbstractConduit.close(AbstractConduit.java:56)at org.apache.cxf.transport.http.HTTPConduit.close(HTTPConduit.java:648)at org.apache.cxf.interceptor.MessageSenderInterceptor$MessageSenderEndingInterceptor.handleMessage(MessageSenderInterceptor.java:62)at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:263)at org.apache.cxf.endpoint.ClientImpl.doInvoke(ClientImpl.java:533)at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:463)at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:366)at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:319)at org.apache.cxf.frontend.ClientProxy.invokeSync(ClientProxy.java:88)at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:134)at $Proxy31.queryByID(Unknown Source)at com.tianxiao.whut.client.ClientTest.main(ClientTest.java:22)Caused by: javax.xml.bind.UnmarshalException - with linked exception:[javax.xml.bind.UnmarshalException: Unable to create an instance of com.tianxiao.whut.domain.User - with linked exception:[java.lang.InstantiationException]]at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.handleStreamException(UnmarshallerImpl.java:434)at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:371)at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:348)at org.apache.cxf.jaxb.JAXBEncoderDecoder.unmarshall(JAXBEncoderDecoder.java:778)... 20 moreCaused by: javax.xml.bind.UnmarshalException: Unable to create an instance of com.tianxiao.whut.domain.User - with linked exception:[java.lang.InstantiationException]at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:662)at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:258)at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.createInstance(UnmarshallingContext.java:629)at com.sun.xml.bind.v2.runtime.unmarshaller.StructureLoader.startElement(StructureLoader.java:185)at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:501)at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:480)at com.sun.xml.bind.v2.runtime.unmarshaller.StAXStreamConnector.handleStartElement(StAXStreamConnector.java:247)at com.sun.xml.bind.v2.runtime.unmarshaller.StAXStreamConnector.bridge(StAXStreamConnector.java:181)at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:369)... 22 moreCaused by: javax.xml.bind.UnmarshalException: Unable to create an instance of com.tianxiao.whut.domain.User - with linked exception:[java.lang.InstantiationException]... 31 moreCaused by: java.lang.InstantiationExceptionat sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(Unknown Source)at java.lang.reflect.Constructor.newInstance(Unknown Source)at com.sun.xml.bind.v2.ClassFactory.create0(ClassFactory.java:122)at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.createInstance(ClassBeanInfoImpl.java:282)at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.createInstance(UnmarshallingContext.java:623)... 28 moreException in thread "main" org.apache.cxf.interceptor.Fault: Unmarshalling Error: Unable to create an instance of com.tianxiao.whut.domain.User at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:141)at $Proxy31.queryByID(Unknown Source)at com.tianxiao.whut.client.ClientTest.main(ClientTest.java:22)Caused by: javax.xml.bind.UnmarshalException - with linked exception:[javax.xml.bind.UnmarshalException: Unable to create an instance of com.tianxiao.whut.domain.User - with linked exception:[java.lang.InstantiationException]]at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.handleStreamException(UnmarshallerImpl.java:434)at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:371)at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:348)at org.apache.cxf.jaxb.JAXBEncoderDecoder.unmarshall(JAXBEncoderDecoder.java:778)at org.apache.cxf.jaxb.JAXBEncoderDecoder.unmarshall(JAXBEncoderDecoder.java:642)at org.apache.cxf.jaxb.io.DataReaderImpl.read(DataReaderImpl.java:156)at org.apache.cxf.interceptor.DocLiteralInInterceptor.handleMessage(DocLiteralInInterceptor.java:109)at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:263)at org.apache.cxf.endpoint.ClientImpl.onMessage(ClientImpl.java:797)at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponseInternal(HTTPConduit.java:1626)at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponse(HTTPConduit.java:1493)at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.close(HTTPConduit.java:1401)at org.apache.cxf.transport.AbstractConduit.close(AbstractConduit.java:56)at org.apache.cxf.transport.http.HTTPConduit.close(HTTPConduit.java:648)at org.apache.cxf.interceptor.MessageSenderInterceptor$MessageSenderEndingInterceptor.handleMessage(MessageSenderInterceptor.java:62)at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:263)at org.apache.cxf.endpoint.ClientImpl.doInvoke(ClientImpl.java:533)at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:463)at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:366)at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:319)at org.apache.cxf.frontend.ClientProxy.invokeSync(ClientProxy.java:88)at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:134)... 2 moreCaused by: javax.xml.bind.UnmarshalException: Unable to create an instance of com.tianxiao.whut.domain.User - with linked exception:[java.lang.InstantiationException]at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:662)at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:258)at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.createInstance(UnmarshallingContext.java:629)at com.sun.xml.bind.v2.runtime.unmarshaller.StructureLoader.startElement(StructureLoader.java:185)at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:501)at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:480)at com.sun.xml.bind.v2.runtime.unmarshaller.StAXStreamConnector.handleStartElement(StAXStreamConnector.java:247)at com.sun.xml.bind.v2.runtime.unmarshaller.StAXStreamConnector.bridge(StAXStreamConnector.java:181)at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:369)... 22 moreCaused by: javax.xml.bind.UnmarshalException: Unable to create an instance of com.tianxiao.whut.domain.User - with linked exception:[java.lang.InstantiationException]... 31 moreCaused by: java.lang.InstantiationExceptionat sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(Unknown Source)at java.lang.reflect.Constructor.newInstance(Unknown Source)at com.sun.xml.bind.v2.ClassFactory.create0(ClassFactory.java:122)at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.createInstance(ClassBeanInfoImpl.java:282)at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.createInstance(UnmarshallingContext.java:623)... 28 more请教各位大虾,这是怎么回事 ,好像变糟糕了 !!!

解决方案

jaxb提供了有这种扩展。(CycleRecoverable接口) 解决循环引用问题,你试试:public abstract class User implements CycleRecoverable{//... public Object onCycleDetected(Context arg0) {  Company temp = new Company ();  temp.setId(id);  return temp;  }}public class Right { }
解决方案二:
在你循环引用的对象中都实现CycleRecoverable 接口,实现onCycleDetected 方法,当出现循环引用时,使用一个temp对象替换当前的this

时间: 2024-10-26 07:25:28

cxf 结合 spring 发布web service时 不能实例化User对象的问题的相关文章

CXF 发布 Web Service

使用CXF框架开发 ①.CXF : xfire–>xfire + celtrix 做web service开发的开源框架 ②.开发Server端: 加入cxf的Jar包即可,其它不需要动 测试CXF支持的数据类型 1.基本类型 –int,float,boolean等 2.引用类型 –String –集合:数组,List, Set, Map –自定义类型 Student @WebService public class DataTypesImpl implements DataTypeWS { 

用cxf公布和调用web service

用cxf发布和调用web service 最近我们的系统需要和一个第三方系统对接,对接的方式是通过web service,所以就学习了一下这方面的东西 用CXF来做web service是比较简单的,本文就简单介绍一下如何一步步发布web service,以及调用现有的web service.另外如果系统已经使用了Spring MVC,那么引入CXF需要额外的步骤,见本人另外一篇博客http://kyfxbl.iteye.com/blog/1432920.如果展现层没有用spring mvc,而

php-PHP调用.NET写的web service时异常,这一般是什么问题。异常错误信息如下

问题描述 PHP调用.NET写的web service时异常,这一般是什么问题.异常错误信息如下 SoapFault exception: [soap:Server] Server was unable to process request. ---> Object reference not set to an instance of an object. in D:wwwindex5.php:23 Stack trace: #0 D:wwwindex5.php(23): SoapClient

webservice-我在用php写web service时,调试总是出现问题

问题描述 我在用php写web service时,调试总是出现问题 我在用php写web service时,调试总是出现问题:Procedure 'tel' not present 解决方案 有可能是php版本问题

asp.net引用web service时,出现无法识别的配置节点applicationSettings解决方法

asp教程.net引用web service时,出现无法识别的配置节点applicationSettings解决方法 web service,在web.config中的节点,出现无法识别的配置节点<applicationSettings>的问题 ApplicationSetting 节点的内容: <applicationSettings>   <TrafficPlanUI.Properties.Settings>     <setting name="T

使用CXF和Spring创建Web服务

引言 在本文中,您将使用 CXF 和 Spring 来构建和开发一个订单处理 Web 服务.该 Web 服务处理或验证客户所下的订单,并返回唯一的订单 ID.阅读本文之后,您将能够应用 CXF 的概念和功能来构建和开发 Web 服务. 系统要求 要运行本文中的示例,请确保已在计算机上安装和设置了以下软件: Java 5 或更高版本 Tomcat 5 或更高版本 Ant 构建工具 CXF 二进制分发版 2.1 安装上述分发版以后,设置以下环境变量: JAVA_HOME(用于 Java) CATAL

CXF+Spring 开发Web Service如何在一个wsdl中配置多个portType

问题描述 如题查了不少网页,都是简单的教程,一般也都是写一个接口,直接发布了<beanid="SayHello"class="com.yxj.CxfWBImpl"/><jaxws:endpointid="sayHello"implementor="#SayHello"address="/abc"/>如果想在一个wsdl中发布多个服务接口,改如何配置呢?下面的abc和abc1如果换成一

C# 调用web Service时 需要Basic身份认证

问题描述 现在有一个局域网的WebService,在IE中打开时需要用户名和密码,输入后可以看到XML.我把WebService以添加WebReference的方式添加到WindowsService项目中,同样需要用户名和密码,输入后可正常引用.windows服务安装后不能启动,原因估计为windows服务不能引用未经验证的WebService,问题我有用户名和密码怎么传递给服务器,验证方式Basic 解决方案 解决方案二:1,使用SoapHeader传递和验证用户WebService端的代码:

发布WEB站点时出现Server Application Unavailable_基础应用

我是参考这个弄好的http://www.cublog.cn/u/8180/showart_171293.html Server Application Unavailable The web application you are attempting to access on this web server is currently unavailable. Please hit the "Refresh" button in your web browser to retry yo