首页 > 代码库 > CXF之"@XmlType.name 和 @XmlType.namespace 为类分配不同的名称"错误

CXF之"@XmlType.name 和 @XmlType.namespace 为类分配不同的名称"错误

CXF 的 wsdl2java.bat 生产的代码,拷贝到目录,进行调研 web service接口时,抛出错误:

 

Exception in thread "main" javax.xml.ws.WebServiceException: org.apache.cxf.service.factory.ServiceConstructionException
at org.apache.cxf.jaxws.ServiceImpl.getPort(ServiceImpl.java:347)
at org.apache.cxf.jaxws.ServiceImpl.getPort(ServiceImpl.java:336)
at javax.xml.ws.Service.getPort(Service.java:119)
at com.hemr.hl7.ws.client2.HL7MessageReveiverImplService.getHL7MessageReveiverImplPort(HL7MessageReveiverImplService.java:72)
at com.hemr.hl7.ws.client2.ClientTest.main(ClientTest.java:7)
Caused by: org.apache.cxf.service.factory.ServiceConstructionException
at org.apache.cxf.jaxb.JAXBDataBinding.initialize(JAXBDataBinding.java:350)
at org.apache.cxf.service.factory.AbstractServiceFactoryBean.initializeDataBindings(AbstractServiceFactoryBean.java:86)
at org.apache.cxf.service.factory.ReflectionServiceFactoryBean.buildServiceFromWSDL(ReflectionServiceFactoryBean.java:446)
at org.apache.cxf.service.factory.ReflectionServiceFactoryBean.initializeServiceModel(ReflectionServiceFactoryBean.java:548)
at org.apache.cxf.service.factory.ReflectionServiceFactoryBean.create(ReflectionServiceFactoryBean.java:265)
at org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean.create(JaxWsServiceFactoryBean.java:215)
at org.apache.cxf.frontend.AbstractWSDLBasedEndpointFactory.createEndpoint(AbstractWSDLBasedEndpointFactory.java:102)
at org.apache.cxf.frontend.ClientFactoryBean.create(ClientFactoryBean.java:91)
at org.apache.cxf.frontend.ClientProxyFactoryBean.create(ClientProxyFactoryBean.java:157)
at org.apache.cxf.jaxws.JaxWsProxyFactoryBean.create(JaxWsProxyFactoryBean.java:142)
at org.apache.cxf.jaxws.ServiceImpl.createPort(ServiceImpl.java:478)
at org.apache.cxf.jaxws.ServiceImpl.getPort(ServiceImpl.java:345)
... 4 more
Caused by: com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 2 counts of IllegalAnnotationExceptions
两个类具有相同的 XML 类型名称 "{http://ws.hl7.hemr.com/}receiveMessage"。请使用 @XmlType.name 和 @XmlType.namespace 为类分配不同的名称。
this problem is related to the following location:
at com.hemr.hl7.ws.client2.jaxws_asm.ReceiveMessage
this problem is related to the following location:
at com.hemr.hl7.ws.client2.ReceiveMessage
at public javax.xml.bind.JAXBElement com.hemr.hl7.ws.client2.ObjectFactory.createReceiveMessage(com.hemr.hl7.ws.client2.ReceiveMessage)
at com.hemr.hl7.ws.client2.ObjectFactory
两个类具有相同的 XML 类型名称 "{http://ws.hl7.hemr.com/}receiveMessageResponse"。请使用 @XmlType.name 和 @XmlType.namespace 为类分配不同的名称。
this problem is related to the following location:
at com.hemr.hl7.ws.client2.jaxws_asm.ReceiveMessageResponse
this problem is related to the following location:
at com.hemr.hl7.ws.client2.ReceiveMessageResponse
at public javax.xml.bind.JAXBElement com.hemr.hl7.ws.client2.ObjectFactory.createReceiveMessageResponse(com.hemr.hl7.ws.client2.ReceiveMessageResponse)
at com.hemr.hl7.ws.client2.ObjectFactory

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:445)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:277)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:124)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1123)
at com.sun.xml.internal.bind.v2.ContextFactory.createContext(ContextFactory.java:147)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:247)
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:234)
at javax.xml.bind.ContextFinder.find(ContextFinder.java:462)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:641)
at org.apache.cxf.common.jaxb.JAXBContextCache.createContext(JAXBContextCache.java:356)
at org.apache.cxf.common.jaxb.JAXBContextCache.getCachedContextAndSchemas(JAXBContextCache.java:244)
at org.apache.cxf.jaxb.JAXBDataBinding.createJAXBContextAndSchemas(JAXBDataBinding.java:493)
at org.apache.cxf.jaxb.JAXBDataBinding.initialize(JAXBDataBinding.java:348)
... 15 more

 

原因是生成代码时指定的包路径和 拷贝过去的包路径不是一致的,比如:

G:\cxf\apache-cxf-3.1.6\bin>wsdl2java -p com.hemr.hl7.ws.client -d G:/cxf -verbose http://localhost:8080/webservice/hL7MessageReveiver?wsdl

比如这样生产的代码,然后将生产的代码拷贝到 包路径: com.hemr.hl7.ws.client.test 下面去,就会抛出上面的错误,

解决办法是,如果要将生产的代码拷贝到  com.hemr.hl7.ws.client.test 去执行,那么需要重新制定 包路径,重新生成代码:

G:\cxf\apache-cxf-3.1.6\bin>wsdl2java -p com.hemr.hl7.ws.client.test -d G:/cxf -verbose http://localhost:8080/webservice/hL7MessageReveiver?wsdl

这样就不会抛出错误了。

CXF之"@XmlType.name 和 @XmlType.namespace 为类分配不同的名称"错误