Organizational Research By

Surprising Reserch Topic

Question:JAX-WS Soap Faults not appearing in WSDL


I am creating a web service using JAX-WS (I am creating this using the Java to WSDL approach).

Im having trouble getting my exception to work as I require.

I have created the following exception class:

@WebFault
public class MyWebServiceException extends SOAPFaultException {

    private static final long serialVersionUID = 8234753208722614057L;

    protected MyWebServiceException(SOAPFault fault) {
        super(fault);
    }

    protected MyWebServiceException(SOAPFault fault, Throwable throwable) {
        this(fault);
        super.setStackTrace(throwable.getStackTrace());
    }
}

This allows me to have the following in my SOAP response:

E0010
Invalid Report

    " rel="nofollow" target="_blank">http://jax-ws.dev.java.net/">
     Invalid Report
     
     

However, because my exception is a SOAPFaultException which extends RuntimeException, it is not being added to the WSDL, so when users of the service generate their client stubs the exception is not included.

Does anyone know how to create this exception to give me the intended output (ie including the faultcode and faultstring) but also appear in the WSDL (I guess it needs to be a checked exception)

I searched SO high and low to come up with what I have already!

asked Sep 13, 2013 in Core java by rajesh
edited Sep 12, 2013
0 votes
42 views



Related Hot Questions

2 Answers

0 votes
If you create a business exception and throw it, you can get error codes in client withoud messing with soap internals.

Your webmethod would look like:

@WebMethod
public String sayHello(@WebParam String name, @WebParam String thing)  throws MyException
{
    // TODO Auto-generated method stub
    if ("err".equals(name))
        throw new MyException("E0010","Report not found");
    return null;
}

Your business exception:

public class MyException extends Exception {

    private static final long serialVersionUID = -923394585528818428L;
    public MyException(String errorCode,String errorMessage)
    {
        this.errorCode = errorCode;
        this.errorMessage = errorMessage;
    }

    public String getErrorCode() {
        return errorCode;
    }
    public void setErrorCode(String errorCode) {
        this.errorCode = errorCode;
    }
    public String getErrorMessage() {
        return errorMessage;
    }
    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }
    String errorCode;
    String errorMessage;

}

 

answered Sep 13, 2013 by rajesh
edited Sep 12, 2013
0 votes
If you create a business exception and throw it, you can get error codes in client withoud messing with soap internals.

Your webmethod would look like:

@WebMethod
public String sayHello(@WebParam String name, @WebParam String thing)  throws MyException
{
    // TODO Auto-generated method stub
    if ("err".equals(name))
        throw new MyException("E0010","Report not found");
    return null;
}

Your business exception:

public class MyException extends Exception {

    private static final long serialVersionUID = -923394585528818428L;
    public MyException(String errorCode,String errorMessage)
    {
        this.errorCode = errorCode;
        this.errorMessage = errorMessage;
    }

    public String getErrorCode() {
        return errorCode;
    }
    public void setErrorCode(String errorCode) {
        this.errorCode = errorCode;
    }
    public String getErrorMessage() {
        return errorMessage;
    }
    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }
    String errorCode;
    String errorMessage;

}

 

answered Sep 13, 2013 by rajesh
edited Sep 12, 2013
And the return onexception will be:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body>
      <soap:Fault>
         <faultcode>soap:Server</faultcode>
         <faultstring>Fault occurred while processing.</faultstring>
         <detail>
            <ns1:MyException xmlns:ns1="http://ws/">
               <errorMessage xmlns:ns2="http://ws/">Report not found</errorMessage>
               <errorCode xmlns:ns2="http://ws/">E0010</errorCode>
            </ns1:MyException>
         </detail>
      </soap:Fault>
   </soap:Body>
</soap:Envelope>

You can also add a stack trace but that would be better to get logged in server, maybe with a GUUID to correlate with client error if needed.
And the return onexception will be:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body>
      <soap:Fault>
         <faultcode>soap:Server</faultcode>
         <faultstring>Fault occurred while processing.</faultstring>
         <detail>
            <ns1:MyException xmlns:ns1="http://ws/">
               <errorMessage xmlns:ns2="http://ws/">Report not found</errorMessage>
               <errorCode xmlns:ns2="http://ws/">E0010</errorCode>
            </ns1:MyException>
         </detail>
      </soap:Fault>
   </soap:Body>
</soap:Envelope>

You can also add a stack trace but that would be better to get logged in server, maybe with a GUUID to correlate with client error if needed.

...