簡體   English   中英

無法創建JAXBContext創建我的wsdl

[英]Unable to create JAXBContext creating my wsdl

我正在嘗試為webservices生成我的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

我在互聯網上看到了一些關於這一點的討論,為所有類構建一個構造函數,直到創建另一個xml規范。 事實是,沒有一個真正的答案或我測試的解決方案不起作用。

我在這里讀到了關於這個問題的討論,但它沒有終止,我不知道如何解決。 如果有人對此有所了解,我非常感謝能將我加入到嚴格的方向以避免這種情況。

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

Throwable拋出的對象(即異常和錯誤)無法直接傳輸,因為它們無法序列化為XML( StackTraceElement沒有no-arg構造函數,這是JAXB所必需的)。

您必須使用SOAP錯誤。 看到這個問題 它指向您應該放在異常類上的@WebFault注釋。 (也可能檢查這個這個

Domenic 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版本

我已經通過將jaxb-impl版本從2.1.9修復到2.2.6解決了這個問題,現在它工作正常

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

它看起來像您聲明的API的一部分包括一個擴展類型的類型,其內部字段類型為StackTraceElement []。

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

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

眾所周知,由於StackTrace沒有默認構造函數,因此JAXB無法編組任何Exception / Throwable類。 你可以做的是創建你自己的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>

請注意,您不必創建自定義異常。 您只需在生成類時使用正確版本的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>

我也有這個問題! 完全一樣!

看到這個錯誤,

 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的第一個答案。

我通過在界面上添加一個SOAPBinding解決了這個問題:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM