3

I have custom exception:

@WebFault(targetNamespace = "http://somenamespace/server/faults")
public class SoapBadRequestException extends Exception {

private static final long serialVersionUID = 1L;

private FaultBean faultBean;

public static class FaultBean {}

public SoapBadRequestException() { super(); }

public SoapBadRequestException(String message, FaultBean faultBean, Throwable cause) {
    super(message, cause);
    this.faultBean = faultBean;
}

public SoapBadRequestException(String message, FaultBean faultBean) {
    super(message);
    this.faultBean = faultBean;
}

public FaultBean getFaultInfo() { return faultBean; }

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

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

And I have plugin configuration:

<plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>jaxb2-maven-plugin</artifactId>
            <version>2.2</version>
            <executions>
                <execution>
                    <id>schemagen-fault</id>
                    <goals>
                        <goal>schemagen</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <transformSchemas>
                    <transformSchema>
                        <uri>http://somenamespace/server/faults</uri>
                        <toPrefix>faults</toPrefix>
                        <toFile>faults_schema.xsd</toFile>
                    </transformSchema>
                </transformSchemas>
                <sources>
                    <source>
                        src/main/java/blabla/SoapBadRequestException.java
                    </source>
                </sources>
            </configuration>
        </plugin>

And I get the following error:

java.lang.StackTraceElement is a non-static inner class, and JAXB can't handle those.
this problem is related to the following location:
    at java.lang.StackTraceElement (Unknown Source)
    at java.lang.Throwable.getStackTrace (Unknown Source)
    at java.lang.Throwable (Unknown Source)
    at java.lang.Exception (Unknown Source)
    at java.lang.RuntimeException (Unknown Source)
    at blabla.SoapBadRequestException(src/main/java/blabla/SoapBadRequestException.java:12)

As you can see, overriding doesn't help. I tried suggestions from here and gooooogled for a long time, but nothing helped.

Community
  • 1
  • 1
Lia
  • 744
  • 1
  • 9
  • 30
  • Did you try Dominic's answer in the question you linked? – Pace Mar 17 '16 at 19:17
  • @Pace I tried this answer: http://stackoverflow.com/a/10354393/3199508 without any success. Maybe it works for jaxws-maven-plugin which generate wsdl, but not for jaxb2-maven-plugin generating xsd. – Lia Mar 18 '16 at 09:05
  • You are right, it appears the parent properties override the child properties. I have looked around and not found a great solution. What version of the JDK are you using? Are you using the built-in JAX-B or supplying your own? – Pace Mar 18 '16 at 17:16
  • @Pace I tried JDK 1.8.0_74 and 1.7.0_60, JAX-B is the built-in one – Lia Mar 21 '16 at 07:04
  • Then I am afraid I, personally, have no good answer for you (though someone else may know). If you weren't using XJC but creating a JAXBContext yourself it might be possible to add some custom logic into the context. With xjc you might be able to create a plugin/extension to handle this case but that seems like a lot of work. Also, as you mentioned, the wsdl gen has a way around this. Can you not use a wsdl? If you weren't using SOAP I would recommend using an XmlAdapter to convert all Throwable objects to some other, more easily serialized, type. – Pace Mar 21 '16 at 19:27
  • I think your best available option is probably to use an XmlAdapter as described here: http://stackoverflow.com/questions/1241551/how-do-you-serialize-an-exception-subclass-in-jaxb – Pace Mar 21 '16 at 19:32

0 Answers0