users@jersey.java.net

[Jersey] Re: To throw or to return "error" code

From: Jakub Podlesak <jakub.podlesak_at_oracle.com>
Date: Tue, 01 Nov 2011 14:05:47 +0100

Hi John,

please see inlines...

On 24.10.2011 19:09, John Smith wrote:
> So i have been following the tutorials with Netbeans simple crud
> stuff. I get it...
>
> Now the question is I'm doing an "add" operation and I want to
> validate the data before...

An obvious option is to validate the instance representing the input
entity either
in your resource method or as part of the add operation. Another option
would
be to use container request filters either directly linked to specific
resource methods (see [1])
or configured generally for all resources (see [2]).


> Should I throw an exception or return an "error" code. I'm thinking I
> should return an error code. Keeping in mind I will also add a
> function that will query the system and return a list of actions (ADD,
> UPDATE, DOSOMETHING) executed and what their final status was.

You can use the response builder (see [1]) to fully control the
response you return back
from your resource method. Another option would be to throw specific
validation exceptions
and introduce a custom exception mapper for them. But then it might be
difficult to put together
the whole operation list as you suggested.


>
> On a side note what's a good way to validate the incoming data. let
> JAXB with XSL do it. or use some sort of 3rd party bean validation API
> like hibernate validation?


It really depends on your needs. You may pretty much end up using both
approaches at the same time,
as XML schema could not suffice to fully guarantee valid input based on
how you define
the input validity.

~Jakub


[1]http://jersey.java.net/nonav/apidocs/latest/jersey/javax/ws/rs/core/Response.ResponseBuilder.html
[2]http://jersey.java.net/nonav/apidocs/latest/jersey/com/sun/jersey/spi/container/ContainerRequestFilter.html
[3]http://jersey.java.net/nonav/apidocs/latest/jersey/com/sun/jersey/spi/container/ResourceFilterFactory.html