Two classes have the same XML type name 排錯【轉】


  今天遇到一個問題,webservice發布的時候報下面的錯誤:

<strong>Caused by: com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
Two classes have the same XML type name "{http://xxx.yyyy.com}createProcessResponse". Use @XmlType.name and @XmlType.namespace to assign different names to them.
    this problem is related to the following location:
        at xxx.yyy.gwfp.ws.dto.CreateProcessResponse
        at private xxx.yyy.gwfp.ws.dto.CreateProcessResponse xxx.yyy.gwfp.ws.jaxws_asm.CreateProcessResponse._return
        at xxx.yyy.gwfp.ws.jaxws_asm.CreateProcessResponse
    this problem is related to the following location:
        at xxx.yyy.gwfp.ws.jaxws_asm.CreateProcessResponse

    at com.sun.xml.bind.v2.runtime.IllegalAnnotationsException$Builder.check(IllegalAnnotationsException.java:102)
    at com.sun.xml.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(JAXBContextImpl.java:472)
    at com.sun.xml.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:302)
    at com.sun.xml.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1140)
    at com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:154)
    at com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:121)
    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.createContext(JAXBDataBinding.java:562)
    at org.apache.cxf.jaxb.JAXBDataBinding.createJAXBContextAndSchemas(JAXBDataBinding.java:502)
    at org.apache.cxf.jaxb.JAXBDataBinding.initialize(JAXBDataBinding.java:322)</strong>

代碼如下

@Stateless
@WebService()
public class AccountWS {

@WebMethod()
public CreateProcessResponse createProcess(@WebParam(name = "request") CreateProcessRequest request) {
    return null;
}

 

起初找不出來原因,后來才發現,由於JAX-WS對webservice里面得每個方法都生成一個類,生成的類名為: methodName + "Response",所以就回導致生成的類和原來的類有兩個相同的xml type。

      知道原因后,除了我們修改方法名外,還有下面的3種解決方法: 

  •  更換返回值對象的name
@XMLType(name="CreateProcessResponseMsg", namespace="http://xxx.yyy.com")
  • 更換返回值對象的namespace
@XMLType(name="CreateProcessResponse", namespace="http://xxx.yyy.com/message")
  •  給方法上加上下面的注解

@WebMethod(operationName="differentFromMethodName")

        其實在最開始的時候說生成的class的名稱是方法名+Response,是不准確的,operationName的默認值就是方法名,其實就是operationName+Response, 這個注解其實會改變生成的類的名稱,對接口是沒有影響的.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM