CXF之"@XmlType.name 和 @XmlType.namespace 为类分配不同的名称"异常
情景描述:在我的webservice服务中,要调用远程写好的一个服务;由于我的后台是用CXF方式开发的,所以调用远程的方式采用的是由wsdl生成本地代理接口来实现的,生成本地接口的代码如下:
?
F:\jar包驱动\apache-cxf-2.6.0\bin>wsdl2java -p com.dyc.client -d d:/ws -verbose http://localhost:7000/gisplatform/SpatialAnalysisService?wsdl
?
然后将生成的在ws目录下的源文件COPY到我的工程中调用,报下面错误
?
信息: Creating Service {http://www.sgcc.com.cn/sggis/service/gisservice}SpatialAnalysisServiceService from class cn.creaway.webgis.proxy.SpatialAnalysisServiceException in thread "main" org.apache.cxf.service.factory.ServiceConstructionExceptionat org.apache.cxf.jaxb.JAXBDataBinding.initialize(JAXBDataBinding.java:297)at org.apache.cxf.service.factory.AbstractServiceFactoryBean.initializeDataBindings(AbstractServiceFactoryBean.java:86)at org.apache.cxf.service.factory.ReflectionServiceFactoryBean.buildServiceFromClass(ReflectionServiceFactoryBean.java:474)at org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean.buildServiceFromClass(JaxWsServiceFactoryBean.java:685)at org.apache.cxf.service.factory.ReflectionServiceFactoryBean.initializeServiceModel(ReflectionServiceFactoryBean.java:536)at org.apache.cxf.service.factory.ReflectionServiceFactoryBean.create(ReflectionServiceFactoryBean.java:248)at org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean.create(JaxWsServiceFactoryBean.java:205)at org.apache.cxf.frontend.AbstractWSDLBasedEndpointFactory.createEndpoint(AbstractWSDLBasedEndpointFactory.java:101)at org.apache.cxf.frontend.ClientFactoryBean.create(ClientFactoryBean.java:90)at org.apache.cxf.frontend.ClientProxyFactoryBean.create(ClientProxyFactoryBean.java:155)at org.apache.cxf.jaxws.JaxWsProxyFactoryBean.create(JaxWsProxyFactoryBean.java:156)at cn.creaway.webgis.business.SpatialAnalysisServiceBusiness.computeUserLineLengthStatisticByDept(SpatialAnalysisServiceBusiness.java:71)at cn.creaway.webgis.business.SpatialAnalysisServiceBusiness.main(SpatialAnalysisServiceBusiness.java:85)Caused by: com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 3 counts of IllegalAnnotationExceptions两个类具有相同的 XML 类型名称 "{http://www.sgcc.com.cn/sggis/service/gisservice}computePSRStatisticByDept"。请使用 @XmlType.name 和 @XmlType.namespace 为类分配不同的名称。this problem is related to the following location:at cn.creaway.webgis.proxy.jaxws_asm.ComputePSRStatisticByDeptthis problem is related to the following location:at cn.creaway.webgis.proxy.ComputePSRStatisticByDeptat public cn.creaway.webgis.proxy.ComputePSRStatisticByDept cn.creaway.webgis.proxy.ObjectFactory.createComputePSRStatisticByDept()at cn.creaway.webgis.proxy.ObjectFactory两个类具有相同的 XML 类型名称 "{http://www.sgcc.com.cn/sggis/service/gisservice}computeLineLengthStatisticByDeptResponse"。请使用 @XmlType.name 和 @XmlType.namespace 为类分配不同的名称。this problem is related to the following location:at cn.creaway.webgis.proxy.jaxws_asm.ComputeLineLengthStatisticByDeptResponsethis problem is related to the following location:at cn.creaway.webgis.proxy.ComputeLineLengthStatisticByDeptResponseat public cn.creaway.webgis.proxy.ComputeLineLengthStatisticByDeptResponse cn.creaway.webgis.proxy.ObjectFactory.createComputeLineLengthStatisticByDeptResponse()at cn.creaway.webgis.proxy.ObjectFactory两个类具有相同的 XML 类型名称 "{http://www.sgcc.com.cn/sggis/service/gisservice}computePSRStatisticByDeptResponse"。请使用 @XmlType.name 和 @XmlType.namespace 为类分配不同的名称。this problem is related to the following location:at cn.creaway.webgis.proxy.jaxws_asm.ComputePSRStatisticByDeptResponsethis problem is related to the following location:at cn.creaway.webgis.proxy.ComputePSRStatisticByDeptResponseat public cn.creaway.webgis.proxy.ComputePSRStatisticByDeptResponse cn.creaway.webgis.proxy.ObjectFactory.createComputePSRStatisticByDeptResponse()at cn.creaway.webgis.proxy.ObjectFactory.......................................
?
开始一直怀疑是因为我项目中的服务名与我调用的服务名之间有冲突,根据异常信息很容易让人如此思考;后来无意中打开一个接口文发现,原理我COPY到工程中时,包的路径改变了,而原接口引用的类路径却还是我生成时用的目录,如下:
......................................................@ResponseWrapper(localName = "computePSRStatisticByDeptResponse", targetNamespace = "http://www.sgcc.com.cn/sggis/service/gisservice", className = "com.dyc.client.ComputePSRStatisticByDeptResponse")public java.lang.String computePSRStatisticByDept(@WebParam(name = "InputXML", targetNamespace = "") java.lang.String inputXML);.......................................................?
可以发现我的className一直引用的是我原来的包名,而我现在的包名是cn.gary.test
?
修改方法,改接口显然不实际,所以重新生成接口,修改命令为:
?
F:\jar包驱动\apache-cxf-2.6.0\bin>wsdl2java -p cn.gary.test -d d:/ws -verbose http://localhost:7000/gisplatform/SpatialAnalysisService?wsdl
?
再将生成的接口COPY到相同的接口下,就不会再报错了。
?
总结:同样的异常也有可能别的原因引起的,这里只是其中一种可能.
?