Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>A WebApplicationException is a way in which you may stop execution of a REST resource and send some meaningful information to your client. For the stuff I have been doing I subclassed this exception so that it has an implementation that produces JSON as error messages to the client. In the event of an error condition, let us say a missing file I might do something like this:</p> <pre><code>}catch(FileNotFoundException ex){ throw new MyException(ex.getMessage()); </code></pre> <p>On the client this then would produce something like:</p> <pre><code>{ errorCode: 56, errorMessage: 'could not find file "input.txt"' }; </code></pre> <p><a href="http://download.oracle.com/javaee/6/api/javax/ws/rs/WebApplicationException.html">http://download.oracle.com/javaee/6/api/javax/ws/rs/WebApplicationException.html</a>'</p> <p>A WebServiceException is the root run time exception for Jersey, i.e. its what most commonly results from your resources crashing and results in a HTTP 500.</p> <p><a href="http://download.oracle.com/javaee/5/api/javax/xml/ws/WebServiceException.html">http://download.oracle.com/javaee/5/api/javax/xml/ws/WebServiceException.html</a></p> <p>So the short answer is the first exception is one you might throw and the other is one you hope is never thrown. :P</p> <p>An example:</p> <pre><code>public class MyException extends WebApplicationException { public MyException(JSONObject jsonObject) { super(Response.status(Response.Status.OK) .entity(jsonObject) .type(MediaType.APPLICATION_JSON) .build()); } </code></pre> <p>Then from anywhere in your code you want to halt execution and send the error information to the client do this:</p> <pre><code>}catch(FileNotFoundException ex){ throw new MyException(new JSONObject(){{ this.put("errorCode", 4); .... }}); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload