问题描述
cxf spring集合中开发Web Service遇见的java.util.Date无缺省函数问题 在domain中有User类public class User { private String userName;private String userPassword;private String mobile;private String email;private String nickname;private String region;private java.sql.Date birthday ;private String telephone;private String address;private String postcode;private String signature;private String imageUrl;private int userID;} 其中birthday类型为java.sql.Date,没有开发web service时,使用spring 和hibernate都OK 加入cxf后报错了 严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListenerorg.springframework.beans.factory.BeanCreationException: Error creating bean with name 'UserWebService1111': Invocation of init method failed; nested exception is javax.xml.ws.WebServiceException: org.apache.cxf.service.factory.ServiceConstructionExceptionat org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1420)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)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:4723)at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5226)at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5221)at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)at java.util.concurrent.FutureTask.run(FutureTask.java:138)at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)at java.lang.Thread.run(Thread.java:662)Caused by: javax.xml.ws.WebServiceException: org.apache.cxf.service.factory.ServiceConstructionExceptionat org.apache.cxf.jaxws.EndpointImpl.doPublish(EndpointImpl.java:350)at org.apache.cxf.jaxws.EndpointImpl.publish(EndpointImpl.java:239)at org.apache.cxf.jaxws.EndpointImpl.publish(EndpointImpl.java:509)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 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeCustomInitMethod(AbstractAutowireCapableBeanFactory.java:1544)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1485)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1417)... 20 moreCaused by: org.apache.cxf.service.factory.ServiceConstructionExceptionat org.apache.cxf.jaxb.JAXBDataBinding.initialize(JAXBDataBinding.java:292)at org.apache.cxf.service.factory.AbstractServiceFactoryBean.initializeDataBindings(AbstractServiceFactoryBean.java:86)at org.apache.cxf.service.factory.ReflectionServiceFactoryBean.buildServiceFromClass(ReflectionServiceFactoryBean.java:444)at org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean.buildServiceFromClass(JaxWsServiceFactoryBean.java:682)at org.apache.cxf.service.factory.ReflectionServiceFactoryBean.initializeServiceModel(ReflectionServiceFactoryBean.java:507)at org.apache.cxf.service.factory.ReflectionServiceFactoryBean.create(ReflectionServiceFactoryBean.java:241)at org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean.create(JaxWsServiceFactoryBean.java:202)at org.apache.cxf.frontend.AbstractWSDLBasedEndpointFactory.createEndpoint(AbstractWSDLBasedEndpointFactory.java:101)at org.apache.cxf.frontend.ServerFactoryBean.create(ServerFactoryBean.java:157)at org.apache.cxf.jaxws.JaxWsServerFactoryBean.create(JaxWsServerFactoryBean.java:202)at org.apache.cxf.jaxws.EndpointImpl.getServer(EndpointImpl.java:433)at org.apache.cxf.jaxws.EndpointImpl.doPublish(EndpointImpl.java:322)... 29 moreCaused by: com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptionsjava.sql.Date does not have a no-arg default constructor.this problem is related to the following location:at java.sql.Dateat public java.sql.Date com.tianxiao.whut.domain.User.getBirthday()at com.tianxiao.whut.domain.Userat private java.util.List com.tianxiao.whut.webservice.jaxws_asm.QueryByNicknameResponse._returnat com.tianxiao.whut.webservice.jaxws_asm.QueryByNicknameResponse at com.sun.xml.bind.v2.runtime.IllegalAnnotationsException$Builder.check(IllegalAnnotationsException.java:106)at com.sun.xml.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(JAXBContextImpl.java:466)at com.sun.xml.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:298)at com.sun.xml.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:141)at com.sun.xml.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1157)at com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:145)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.JAXBContextCache.createContext(JAXBContextCache.java:260)at org.apache.cxf.jaxb.JAXBContextCache.getCachedContextAndSchemas(JAXBContextCache.java:169)at org.apache.cxf.jaxb.JAXBDataBinding.createJAXBContextAndSchemas(JAXBDataBinding.java:418)at org.apache.cxf.jaxb.JAXBDataBinding.initialize(JAXBDataBinding.java:290)... 40 more 请问这个该如何解决?
解决方案
引用java.sql.Date does not have a no-arg default constructor.也就是说java.sql.Date这个类是不能这样创建实例的java.sql.Date date = new java.sql.Date();要用java.sql.Date date = new java.sql.Date(new java.util.Date().getTime() );
解决方案二:
好像只能用java.util.Date sql 不能序列
解决方案三:
其实是可以支持java.sql.Date的,这和java.util.Date区别并不大,能使用java对象,所有的java对象都应该可以使用。你的问题应该出在你调用 QueryByNickname接口返回List时,其中构造对象时使用了无参数的java.sql.Date类型,你检查一下代码。
解决方案四:
Web Service 在进行接口封装过程中,遇到调用实体类的情况,接口会自动去识别实体类数据的基本类型。例如 int、String等。在此,我们需要注意的是,有些数据类型是不能被web service 所识别的。其中常见的日期类型,如果我们的基本类型采用java.sql.Date的话是会报错的(报错信息类似于如下所示)。 而应当用java.util.Date类。