Unable to create JAXBContext creating my wsdl
Asked Answered
W

8

20

I am trying to generate my WSDL for webservices but I get this error:

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

I saw a few discusion in internet about this begin with build a constructor for all classes until create another xml specification. The truth is there is no a real answer for this or the solutions that I test doesnt work any.

I read a discussion here about this problem but it was unterminated and I dont know how to resolve. If somebody has a cluee about this I so appreciated to addrees me into rigth direction to avoid this.

I am using Debian Squezze, Java 1.6_20, JAX-WS JAX-WS RI 2.1.6 in JDK 6 and wsgen to generate the wsdl. The first step it did correct, generate the jaxws directory with bean classes.

Wildfowl answered 29/8, 2011 at 15:36 Comment(2)
This might be related: bugs.sun.com/bugdatabase/view_bug.do?bug_id=6974244Tollman
@KevinWong: that link appears broken now...Brahmana
L
16

Throwable objects (that means exceptions and errors) can't be transferred directly, because they can't be serialized to XML (the StackTraceElement doesn't have a no-arg constructor, which is required by JAXB).

You have to use SOAP faults for that. See this question. It points you to the @WebFault annotation which you should put on your exception class. (Also probably check this and this)

Loquacity answered 26/10, 2011 at 7:27 Comment(1)
Don't know about the author, but this was the correct answer for me. It got me rolling a half a day wasted.Pentose
P
11

Domenic D. had the right answer that worked for me, but I would like to expand on it.

My Maven project built just fine with JDK 6, but when I switched to be using JDK 1.7 as my default, the build broke. By adding an explicit jaxb-impl dependency w/ version, it worked, such as:

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

Regarding the version: This bug is listed at https://java.net/jira/browse/JAXB-814. It's fixed for versions 2.2.4u2, 2.2.5, 2.3

Pastoralize answered 25/2, 2013 at 17:28 Comment(1)
Updated to "com.sun.xml.ws:jaxws-rt:2.2.10" and it's OK now. Thanks!Bodily
U
9

I have resolved this issue by fixing the jaxb-impl version from 2.1.9 to 2.2.6 and it works fine now

<dependency>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-impl</artifactId>
        <version>2.2.6</version>
</dependency>
Unclog answered 4/2, 2015 at 11:14 Comment(1)
if you got some strange behavior with stack trace even with 2.2.6, look at my answer (workaround in fact) here: #22112914Conwell
B
3

It looks like a part of your declared API includes a type that extends a type with an internal field of type StackTraceElement[ ].

Because StackTraceElement has no zero-argument constructor, JAXB cannot de-serialize instances of this class .

Try removing the nextException field from SQLExceptionBean, or removing parameters of type SQLExceptionBean from your service APIs.

Boycie answered 25/10, 2011 at 17:37 Comment(0)
T
3

Note, that you don't have to create a custom Exception. You just ned to use the right version of JAXB when you are generating your classes. For example:

<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>
Topic answered 23/7, 2012 at 15:50 Comment(0)
T
2

It is known that any Exception/Throwable class can not be marshalled by JAXB due to the fact that StackTrace does not have a default constructor. What you can do is create your own Exception class that extends from say in your example, WebServiceException. In your class, override the methods that would use StackTrace and add the @XmlTransient annotation to them. As long as you are using JAXB 2.2.4u2+ the Marshaller will obey the @XmlTrasient annotation in your custom class.

Additional detail is available at http://java.net/jira/browse/JAXB-814 that outlines the defect where @XmlTransient didn't work well on subclasses.

Below is a sample class that will marshall properly:

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

}

If you use Maven, you'll need this dependency:

<dependency>
   <groupId>com.sun.xml.bind</groupId>
   <artifactId>jaxb-impl</artifactId>
   <version>2.2.5</version>
</dependency>
Topic answered 27/4, 2012 at 16:24 Comment(0)
C
1

I had this problem too! EXACTLY the same one!

SEE THIS ERROR,

 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

I went through my code,and checked out the reason is

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

A web method should not throw a Exception,remove will solve this problem simplely,if u insist to do that,follow the first answer by @Bozho .

Craft answered 30/6, 2014 at 2:28 Comment(0)
A
-1

I solved this issue by adding a SOAPBinding on top of my interface:

public interface xxxWebServiceImpl
{
    @SOAPBinding(style = SOAPBinding.Style.RPC)
}
Avlona answered 18/7, 2017 at 13:9 Comment(1)
what are you on about please clarify your using 'my:' and really bad grammar / EnglishEndoscope

© 2022 - 2024 — McMap. All rights reserved.