2011-08-29 18 views
18

Sto cercando di generare il mio WSDL per webservices ma ottengo questo errore:Impossibile creare JAXBContext creare la mia 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 

Ho visto alcune discussione in internet su questo comincerà con costruire un costruttore per tutte le classi fino a crea un'altra specifica xml. La verità è che non esiste una risposta reale per questo o le soluzioni che collaudo non funzionano.

Ho letto una discussione qui su questo problema ma è stato sterminato e non so come risolvere. Se qualcuno ha un indizio su questo, ho apprezzato di avermi seguito in questa direzione per evitare questo.

Sto usando Debian Squezze, Java 1.6_20, JAX-WS JAX-WS RI 2.1.6 in JDK 6 e wsgen per generare wsdl. Il primo passo è stato corretto, generare la directory jaxws con le classi bean.

+2

Questo potrebbe essere correlato: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6974244 –

+1

@KevinWong: quel link non sembra essere funzionante ora ... –

risposta

3

Sembra che una parte dell'API dichiarata includa un tipo che estende un tipo con un campo interno di tipo StackTraceElement [].

Poiché StackTraceElement non ha un costruttore di argomenti con zero, JAXB non può deserializzare le istanze di questa classe.

Provare a rimuovere il campo nextException da SQLExceptionBean o rimuovere i parametri di tipo SQLExceptionBean dalle API dei servizi.

14

Throwable oggetti (che significa eccezioni ed errori) non possono essere trasferiti direttamente, perché non possono essere serializzati in XML (il StackTraceElement non ha un costruttore no-arg, che è richiesto da JAXB).

È necessario utilizzare gli errori SOAP per questo. Vedi this question. Indica l'annotazione @WebFault che dovresti inserire nella tua classe di eccezione. (Probabilmente anche controllare this e this)

+0

non so circa il autore, ma questa era la risposta corretta per me. Mi ha fatto rotolare una mezza giornata sprecata. – homaxto

2

È noto che qualsiasi classe Exception/Throwable non può essere marshallata da JAXB a causa del fatto che StackTrace non ha un costruttore predefinito. Quello che puoi fare è creare la tua classe Exception che si estende da say nel tuo esempio, WebServiceException. Nella classe, sovrascrivi i metodi che utilizzano StackTrace e aggiungi l'annotazione @XmlTransient. Finché si utilizza JAXB 2.2.4u2 +, Marshaller obbedisce all'annotazione @XmlTrasient nella classe personalizzata.

Ulteriori dettagli sono disponibili al numero http://java.net/jira/browse/JAXB-814 che delinea il punto in cui @XmlTransient non ha funzionato bene nelle sottoclassi.

Qui di seguito è una classe di esempio che sarà marshall correttamente:

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(); 
    } 

} 

Se si utilizza Maven, avrete bisogno di questa dipendenza:

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

nota, che non si deve creare un'eccezione personalizzata. Devi solo usare la giusta versione di JAXB quando generi le tue classi. Per esempio:

<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

Domenico D. aveva la risposta giusta che ha funzionato per me (io sono troppo nuovo a votare in su che sembra stupido che devo creare un altro post, invece di dire semplicemente funziona un risposta esistenti. ..)

Il mio progetto Maven ha funzionato bene con JDK 6, ma quando sono passato a utilizzare JDK 1.7 come predefinito, il build si è rotto. Con l'aggiunta di un esplicito JAXB-impl dipendenza w/versione, ha funzionato, come ad esempio:

<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> 

Per quanto riguarda la versione. Questo bug è elencato a https://java.net/jira/browse/JAXB-814. È stato risolto per le versioni 2.2.4u2, 2.2.5, 2.3

+0

Aggiornato a "com.sun.xml.ws:jaxws-rt:2.2.10" ed è OK ora. Grazie! –

1

Ho avuto anche questo problema! ESATTAMENTE la stessa!

visualizzare questo errore,

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 

Sono andato con il mio codice, e controllato la ragione è

@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{ 

Metodo web non dovrebbe un'eccezione, rimuovere risolverà questo problema simplely, se insisti per farlo, segui la prima risposta di @Bozho.

4

ho risolto questo problema fissando la versione JAXB-impl da 2.1.9 a 2.2.6 e funziona bene ora

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

se hai qualche strano comportamento con lo stack trace anche con 2.2.6, guarda la mia risposta (soluzione di fatto) qui: https://stackoverflow.com/questions/22112414/java-lang-stacktraceelement-does-non-have -a-no-arg-default-constructor/45503375 # 45503375 – boly38

-1

Ho risolto questo problema con l'aggiunta di un SOAPBinding in cima alla mia interfaccia :

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

di cosa stai parlando per favore chiarisci il tuo utilizzo di "my:" e grammatica veramente pessima/inglese –

Problemi correlati