[jboss-user] [JBoss Web Services] - Re: Custom exceptions with @WebFault

John Fitzpatrick do-not-reply at jboss.com
Thu Jan 19 17:25:29 EST 2012


John Fitzpatrick [https://community.jboss.org/people/jfitzpatrick] created the discussion

"Re: Custom exceptions with @WebFault"

To view the discussion, visit: https://community.jboss.org/message/647717#647717

--------------------------------------------------------------
Have you tried defining the fault in your wsdl file and then using wsimport to have the JAXB artifacts generated for you?

Example that I think should work:

wsdl:

<?xml version="1.0" encoding="UTF-8"?>

<definitions
    name="SubtractNumbers"
    targetNamespace=" http://duke.example.org http://duke.example.org"
    xmlns:tns=" http://duke.example.org http://duke.example.org"
    xmlns=" http://schemas.xmlsoap.org/wsdl/ http://schemas.xmlsoap.org/wsdl/"
    xmlns:xsd=" http://www.w3.org/2001/XMLSchema http://www.w3.org/2001/XMLSchema"
    xmlns:soap=" http://schemas.xmlsoap.org/wsdl/soap/ http://schemas.xmlsoap.org/wsdl/soap/">
    <types>
        <xsd:schema
            xmlns=" http://www.w3.org/2001/XMLSchema http://www.w3.org/2001/XMLSchema"
            targetNamespace=" http://duke.example.org http://duke.example.org"
            elementFormDefault="qualified">

            <complexType name="subtractNumbersResponse">
                <sequence>
                    <element name="return" type="xsd:int" />
                </sequence>
            </complexType>
            <element name="subtractNumbersResponse" type="tns:subtractNumbersResponse"/>

            <complexType name="subtractNumbers">
                <sequence>
                    <element name="arg0" type="xsd:int" />
                    <element name="arg1" type="xsd:int" />
                </sequence>
            </complexType>
            <element name="subtractNumbers" type="tns:subtractNumbers"/>

            <element name="permissionDeniedFault">
                <complexType>
                    <sequence>
                        <element name="message" type="xsd:string"/>
                    </sequence>
                </complexType>
            </element>

        </xsd:schema>
    </types>
    <message name="subtractNumbers">
        <part name="parameters" element="tns:subtractNumbers" />
    </message>
    <message name="subtractNumbersResponse">
        <part name="result" element="tns:subtractNumbersResponse" />
    </message>
    <message name="permissionDeniedException">
        <part name="permissionDeniedFault" element="tns:permissionDeniedFault"/>
    </message>
    <portType name="SubtractNumbersPortType">
        <operation name="subtractNumbers">
            <input message="tns:subtractNumbers" />
            <output message="tns:subtractNumbersResponse" />
            <fault name="permissionDeniedFault" message="tns:permissionDeniedException" />
        </operation>
    </portType>
    <binding name="SubtractNumbersBinding" type="tns:SubtractNumbersPortType">
        <soap:binding transport=" http://schemas.xmlsoap.org/soap/http http://schemas.xmlsoap.org/soap/http" style="document" />
        <operation name="subtractNumbers">
            <soap:operation soapAction="" />
            <input>
                <soap:body use="literal" />
            </input>
            <output>
                <soap:body use="literal" />
            </output>
            <fault name="permissionDeniedFault">
                <soap:fault use="literal" name="permissionDeniedFault" />
            </fault>
        </operation>
    </binding>
    <service name="SubtractNumbersService">
        <port name="SubtractNumbersPort" binding="tns:SubtractNumbersBinding">
            <soap:address location="REPLACE_WITH_ACTUAL_URL" />
        </port>
    </service>
</definitions>

Generated fault/exception related artificats:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "message"
})
@XmlRootElement(name = "permissionDeniedFault")
public class PermissionDeniedFault {

    @XmlElement(required = true)
    protected String message;

    /**
     * Gets the value of the message property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getMessage() {
        return message;
    }

    /**
     * Sets the value of the message property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setMessage(String value) {
        this.message = value;
    }

}

@WebFault(name = "permissionDeniedFault", targetNamespace = " http://duke.example.org http://duke.example.org")
public class PermissionDeniedException
    extends Exception
{

    /**
     * Java type that goes as soapenv:Fault detail element.
     * 
     */
    private PermissionDeniedFault faultInfo;

    /**
     * 
     * @param message
     * @param faultInfo
     */
    public PermissionDeniedException(String message, PermissionDeniedFault faultInfo) {
        super(message);
        this.faultInfo = faultInfo;
    }

    /**
     * 
     * @param message
     * @param faultInfo
     * @param cause
     */
    public PermissionDeniedException(String message, PermissionDeniedFault faultInfo, Throwable cause) {
        super(message, cause);
        this.faultInfo = faultInfo;
    }

    /**
     * 
     * @return
     *     returns fault bean: provider.server.PermissionDeniedFault
     */
    public PermissionDeniedFault getFaultInfo() {
        return faultInfo;
    }

}

Implementation class:

@WebService (serviceName = "SubtractNumbersService",
        portName = "SubtractNumbersPort",
        endpointInterface = "provider.server.SubtractNumbersPortType" )
public class  SubtractNumbersImpl {

    public int subtractNumbers(int arg1, int arg2) throws PermissionDeniedException {

          PermissionDeniedFault permissionDeniedFault = new PermissionDeniedFault();
          permissionDeniedFault.setMessage("Your custom message here explain why permission was denied.");

         throw new PermissionDeniedException("Permission denied.", permissionDeniedFault);
    }
}
--------------------------------------------------------------

Reply to this message by going to Community
[https://community.jboss.org/message/647717#647717]

Start a new discussion in JBoss Web Services at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2044]

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/jboss-user/attachments/20120119/c0156222/attachment-0001.html 


More information about the jboss-user mailing list