Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Throwing custom exceptions in JAX-WS

Tags:

java

jax-ws

I have a JAX-WS web service with a web method that may throw an exception.

@WebMethod
public Folder getTree() throws UnauthorizedException {
    //...
    // Get authorization data..
    //...
    if (!authorized) {
        throw new UnauthorizedException();
    }
    //...
}

It works all right if user is authorized, but when the exception is thrown it doesn't generate SOAP message with a fault, it just crashes web service with

SEVERE: Unauthorized
    ru.cos.xdoc.storage.UnauthorizedException: Unauthorized
    at ru.cos.xdoc.storage.Storage.getTree(Storage.java:136)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    ...

and closes connection

HTTP/1.1 500 Internal Server Error
X-Powered-By: Servlet/2.5
Server: Sun GlassFish Enterprise Server v2.1.1
Content-Type: text/xml;charset="utf-8"
Transfer-Encoding: chunked
Date: Mon, 20 Sep 2010 15:43:59 GMT
Connection: close  

I feel like I miss something simple

EDIT The problem has proven to arise only in the case an exception is thrown not long after the beginning of web method. When a delay is introduced before throwing an exception, like

try {
    Thread.sleep(3000);
} catch (InterruptedException e) {
    e.printStackTrace();
}

throw new UnauthorizedException();

everything works fine.

Does anybody have a clue what may cause such a strange behaviour?

like image 565
greycat Avatar asked Dec 09 '25 04:12

greycat


1 Answers

Have you mapped your exception to a Fault? See JAX-WS - Map Exceptions to faults

like image 166
gpeche Avatar answered Dec 10 '25 17:12

gpeche



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!