Checked exceptions hierarchy in WebServices using JAX-WS Maven Plugin (wsimport)

2.4k views Asked by At

I'm working on a project where we want to use checked exceptions to notify user of (for example) wrong input or wrong action taken. Such exceptions should have hierarchy such as:

public abstract class BusinessException extends java.lang.Exception {...}
public class InvalidInputException extends BusinessException {...}
public class InvalidActionException extends BusinessException {...}

We generate java code from WSDL/XSD (contract-first approach) using Maven, jaxws-maven-plugin, goal wsimport.

I've tried to go along this ( http://www.ibm.com/developerworks/xml/library/ws-tip-jaxrpc.html ) tutorial (it is for jax-rpc, but seems to work for jax-ws as well). I wrote

<definitions ...>

    <message name="empty"/>
    <message name="ExceptionMessage">
        <part name="fault" element="ows:ValidationException"/>
    </message>

    <portType name="TestWebService">
        <operation name="throwException">
            <input message="tns:empty"/>
            <output message="tns:empty"/>
            <fault name="fault" message="tns:ExceptionMessage"/>
        </operation>
    </portType>

    <binding name="TestWebServicePortBinding"
             type="tns:TestWebService">
        <soap:binding transport="http://schemas.xmlsoap.org/soap/http"
                      style="document"/>


        <operation name="throwException">
            <input>
                <soap:body use="literal"/>
            </input>
            <output>
                <soap:body use="literal"/>
            </output>
            <fault name="fault">
                <soap:fault name="fault" use="literal"/>
            </fault>
        </operation>
    </binding>

  ...
</definitions>

with types defined in ows: namespace

<xs:complexType name="BusinessException" abstract="true">
    <xs:sequence>
        <xs:element name="code" type="xs:int"/>
    </xs:sequence>
</xs:complexType>

<xs:complexType name="InvalidInputException">
    <xs:complexContent>
        <xs:extension base="tns:BusinessException">
            <xs:sequence>
                <xs:element name="validationMessage" type="xs:string"/>
            </xs:sequence>
        </xs:extension>
    </xs:complexContent>
</xs:complexType>

<xs:complexType name="InvalidActionException">
    <xs:complexContent>
        <xs:extension base="tns:BusinessException">
            <xs:sequence>
                <xs:element name="actionName" type="xs:string"/>
            </xs:sequence>
        </xs:extension>
    </xs:complexContent>
</xs:complexType>

<xs:element name="ValidationException" type="tns:InvalidInputException"/>

When i run mvn clean package, i get the following (getters, setters, ctors and annotations removed):

public interface TestWebService {
    @WebMethod
    public void throwException() throws ExceptionMessage;
}

public class ExceptionMessage extends Exception {
    private InvalidInputException faultInfo;
    (...)
}

public abstract class BusinessException implements Serializable {
    protected int code;
    (...)
}  

public class InvalidActionException extends BusinessException implements Serializable {
    protected String actionName;
    (...)
}

public class InvalidInputException extends BusinessException implements Serializable {
    protected String validationMessage;
    (...)
}

Which is not what i wanted, because there is one exception and it can hold different faultInto data. Is there a way to create Exception hierarchy as noted above purely in XSD/WSDL? As the ExceptionMessage class is generated directly from <message> tag, I was not able to find a way of creating a parent/child there.

2

There are 2 answers

0
Ondrej Skalicka On BEST ANSWER

It seems that this is not possible using technologies mentioned above (WSDL/XSD (contract-first approach) using Maven, jaxws-maven-plugin, wsimport goal).

2
Pace On

Could you change the WSDL operation to...

<portType name="TestWebService">
    <operation name="throwException">
        <input message="tns:empty"/>
        <output message="tns:empty"/>
        <fault name="fault" message="tns:BusinessException"/>
    </operation>
</portType>