2011-08-29 45 views
18

我試圖生成我的web服務的WSDL,但我得到這個錯誤:無法創建JAXBContext而創建我的WSDL

 
Note: ap round: 2 
Exception in thread "main" javax.xml.ws.WebServiceException: Unable to create JAXBContext 
     at com.sun.xml.internal.ws.model.AbstractSEIModelImpl.createJAXBContext(AbstractSEIModelImpl.java:153) 
     at com.sun.xml.internal.ws.model.AbstractSEIModelImpl.postProcess(AbstractSEIModelImpl.java:83) 
     at com.sun.xml.internal.ws.model.RuntimeModeler.buildRuntimeModel(RuntimeModeler.java:244) 
     at com.sun.tools.internal.ws.wscompile.WsgenTool.buildModel(WsgenTool.java:229) 
     at com.sun.tools.internal.ws.wscompile.WsgenTool.run(WsgenTool.java:112) 
     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 com.sun.tools.internal.ws.Invoker.invoke(Invoker.java:105) 
     at com.sun.tools.internal.ws.WsGen.main(WsGen.java:41) 
Caused by: java.security.PrivilegedActionException: com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions 
java.lang.StackTraceElement does not have a no-arg default constructor. 
     this problem is related to the following location: 
       at java.lang.StackTraceElement 
       at public java.lang.StackTraceElement[] java.lang.Throwable.getStackTrace() 
       at java.lang.Throwable 
       at java.lang.Exception 
       at java.sql.SQLException 
       at private java.sql.SQLException wsdb.jaxws.SQLExceptionBean.nextException 
       at wsdb.jaxws.SQLExceptionBean 

     at java.security.AccessController.doPrivileged(Native Method) 
     at com.sun.xml.internal.ws.model.AbstractSEIModelImpl.createJAXBContext(AbstractSEIModelImpl.java:140) 
     ... 10 more 
Caused by: com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions 
java.lang.StackTraceElement does not have a no-arg default constructor. 
     this problem is related to the following location: 
       at java.lang.StackTraceElement 
       at public java.lang.StackTraceElement[] java.lang.Throwable.getStackTrace() 
       at java.lang.Throwable 
       at java.lang.Exception 
       at java.sql.SQLException 
       at private java.sql.SQLException wsdb.jaxws.SQLExceptionBean.nextException 
       at wsdb.jaxws.SQLExceptionBean 

     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:436) 
     at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.(JAXBContextImpl.java:277) 
     at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1100) 
     at com.sun.xml.internal.bind.v2.ContextFactory.createContext(ContextFactory.java:143) 
     at com.sun.xml.internal.bind.api.JAXBRIContext.newInstance(JAXBRIContext.java:95) 
     at com.sun.xml.internal.ws.developer.JAXBContextFactory$1.createJAXBContext(JAXBContextFactory.java:97) 
     at com.sun.xml.internal.ws.model.AbstractSEIModelImpl$1.run(AbstractSEIModelImpl.java:148) 
     at com.sun.xml.internal.ws.model.AbstractSEIModelImpl$1.run(AbstractSEIModelImpl.java:140) 
     ... 12 more 

我看到網上關於這幾個discusion與建設開始構造所有的班級,直到創建另一個xml規範。事實是,這沒有一個真正的答案或我測試的解決方案不起作用。

我在這裏讀到關於這個問題的討論,但它沒有終止,我不知道如何解決。如果有人對此有所瞭解,我很高興能將我加入嚴謹的方向以避免這種情況發生。

我在JDK 6中使用Debian Squezze,Java 1.6_20,JAX-WS JAX-WS RI 2.1.6和wsgen來生成wsdl。它正確的第一步,用bean類生成jaxws目錄。

+2

這可能與:http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6974244 –

+1

@KevinWong:該鏈接現在看來破... –

回答

3

它看起來像你聲明的API的一部分包含一個類型,它擴展了一個類型爲StackTraceElement []的內部字段的類型。

由於StackTraceElement沒有零參數構造函數,因此JAXB無法反序列化此類的實例。

嘗試從SQLExceptionBean中除去nextException字段,或從服務API中刪除SQLExceptionBean類型的參數。

14

Throwable對象(即異常和錯誤)無法直接傳輸,因爲它們無法序列化爲XML(StackTraceElement沒有JAXB所需的無參數構造函數)。

您必須爲此使用SOAP錯誤。見this question。它指向您應注意您的異常類的@WebFault註釋。 (也可能檢查thisthis

+0

不知道的作者,但這是我的正確答案。它讓我浪費了半天的時間。 – homaxto

2

已知任何Exception/Throwable類都不能由JAXB編組,因爲StackTrace沒有默認構造函數。你可以做的是創建你自己的Exception類,該類繼承自你的例子WebServiceException。在你的類中,重寫將使用StackTrace的方法並將@XmlTransient註釋添加到它們。只要您使用的是JAXB 2.2.4u2 +,Marshaller就會遵循您的自定義類中的@XmlTrasient註釋。

有關詳細信息,請參閱http://java.net/jira/browse/JAXB-814,其中概述了@XmlTransient在子類上不能正常工作的缺陷。

下面是將馬歇爾正確的樣本類:

package com.stackoverflow.7232331; 

import javax.ws.rs.core.Response.Status; 
import javax.xml.bind.annotation.XmlRootElement; 
import javax.xml.bind.annotation.XmlTransient; 

@XmlRootElement 
public class CustomException extends RuntimeException { 

    /** 
    * 
    */ 
    private static final long serialVersionUID = -672804070691541883L; 
    protected String reason; 
    protected Status status; 
    protected int errorCode; 

    public String getReason() { 
     return reason; 
    } 

    public void setReason(String reason) { 
     this.reason = reason; 
    } 

    public Status getStatus() { 
     return status; 
    } 

    public void setStatus(Status status) { 
     this.status = status; 
    } 

    public int getErrorCode() { 
     return errorCode; 
    } 

    public void setErrorCode(int errorCode) { 
     this.errorCode = errorCode; 
    } 

    public CustomException(Status status, String message, String reason, int errorCode) { 
     super(message); 
     this.reason = reason; 
     this.status = status; 
     this.errorCode = errorCode; 
    } 

    public CustomException() { 
     super(); 
    } 

    @XmlTransient 
    @Override 
    public StackTraceElement[] getStackTrace() { 
     return super.getStackTrace(); 
    } 

    @XmlTransient 
    @Override 
    public Throwable getCause() { 
     return super.getCause(); 
    } 

} 

如果你使用Maven,你需要這種依賴性:

<dependency> 
    <groupId>com.sun.xml.bind</groupId> 
    <artifactId>jaxb-impl</artifactId> 
    <version>2.2.5</version> 
</dependency> 
2

注意,你不必創建自定義異常。當您正在生成類時,您只需要使用正確版本的JAXB。例如:

<plugin> 
    <groupId>org.codehaus.mojo</groupId> 
    <artifactId>jaxws-maven-plugin</artifactId> 
    <version>1.12</version> 
    <dependencies> 
     <dependency> 
      <groupId>com.sun.xml.bind</groupId> 
      <artifactId>jaxb-impl</artifactId> 
      <version>2.2.5</version> 
     </dependency> 
    </dependencies> 
    <executions> 
     <execution> 
      ... 
     </execution> 
    </executions> 
</plugin> 
7

多米尼克D.有這樣的工作對我來說是正確的答案(我太新投上一票,這似乎愚蠢,我要創建另一個職位,而不是隻說現有的應答作品。 ..)

我的Maven項目與JDK 6一起構建得很好,但是當我切換到使用JDK 1.7作爲默認設置時,構建打破了。通過增加一個明確的JAXB IMPL依賴W /版本,它的工作,如:

<plugin> 
    <groupId>org.jvnet.jax-ws-commons</groupId> 
    <artifactId>jaxws-maven-plugin</artifactId> 
    <version>2.1</version> 
    <dependencies> 
     <dependency> 
      <groupId>com.sun.xml.bind</groupId> 
      <artifactId>jaxb-impl</artifactId> 
      <version>2.2.6</version> 
     </dependency> 
    </dependencies> 
    <executions> 
     <execution> 
     ... 
     </execution> 
    </executions> 
</plugin> 

關於版本。此錯誤列於https://java.net/jira/browse/JAXB-814。這是固定的版本2.2.4u2,2.2.5,2.3

+0

已更新至「com.sun.xml.ws:jaxws-rt:2.2.10」,現在可以。謝謝! –

1

我也有這個問題呢! 完全相同!

看到此錯誤,

at java.lang.StackTraceElement 
      at public java.lang.StackTraceElement[] java.lang.Throwable.getStackTrace() 
      at java.lang.Throwable 
      at java.lang.Exception 
      at java.sql.SQLException 
      at private java.sql.SQLException wsdb.jaxws.SQLExceptionBean.nextException 
      at wsdb.jaxws.SQLExceptionBean 

我通過我的代碼去了,簽出的原因

@WebMethod(operationName = "reportnewplace") 
public String reportnewplace(@WebParam(name = "xcmc") String xcmc, 
     @WebParam(name = "address") String address, 
     @WebParam(name = "lon") String lon, 
     @WebParam(name = "lat") String lat, 
     @WebParam(name = "UID") String UID) throws SQLException{ 

Web方法不應該拋出一個異常,刪除會簡單地解決這個問題,如果你堅持這樣做,請按照@Bozho的第一個答案。

4

我已經從2.1.9 JAXB的IMPL版固定2.2.6 解決這個問題,現在的工作罰款

<dependency> 
     <groupId>com.sun.xml.bind</groupId> 
     <artifactId>jaxb-impl</artifactId> 
     <version>2.2.6</version> 
</dependency> 
+0

如果你在使用2.2.6的時候遇到了一些奇怪的堆棧跟蹤行爲,請看我的答案(事實上的解決方法):https://stackoverflow.com/questions/22112414/java-lang-stacktraceelement-does-not-have -a-no-arg-default-constructor/45503375#45503375 – boly38

-1

我通過我的界面的頂部上添加SOAPBinding解決了這個問題:

public interface xxxWebServiceImpl 
{ 
    @SOAPBinding(style = SOAPBinding.Style.RPC) 
} 
+0

你在說什麼,請澄清你的'我的:'和真正糟糕的語法/英語 –