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

2

6

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.

Jonis answered 4/2, 2011 at 11:6 Comment(2)
did you ever find another solution for this? I am facing more or less the same issueDenunciation
no, see my answer below. As far as I found out you cannot do this in contract-first approach with jaxws.Jonis
J
0

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

Jonis answered 14/2, 2012 at 7:49 Comment(0)
A
0

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>
Affettuoso answered 9/2, 2011 at 4:54 Comment(2)
I can change it so, but wil this help me create the exception inheritance? (eg. InvalidInputException extends BusinessException)Jonis
It looks like your generated code has the correct inheritance. The only problem is that your throwException method is declaring that it throws ExceptionMessage instead of BusinessException.Affettuoso
J
0

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

Jonis answered 14/2, 2012 at 7:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.