users@jersey.java.net

[Jersey] Re: Handling of parameters exceeding datatype in endpoint?

From: Viggo Navarsete <viggo.navarsete_at_gmail.com>
Date: Mon, 21 Feb 2011 08:32:40 +0100

Thanks for feedback:)

Ideally I don't want to do anything special with this kind of validation, I
would ideally like the framework (Jersey in this case) to handle illegal
values for datatypes and respond correctly to it. I don't want a 404 page in
such cases, and I think it's sad that this is something that I have to take
care of. I would preferably like this to be a "feature" of the framework.

Is it possible?

Regards,
Viggo Navarsete

On Sun, Feb 20, 2011 at 4:30 PM, Mark Petrovic <mspetrovic_at_gmail.com> wrote:

> You may also be able to use an injectable provider like this to pre-digest
> the input before your resource method starts its work:
>
> http://codahale.com/what-makes-jersey-interesting-injection-providers/
>
> Basically, you can inject a custom type as long as you tell Jersey how to
> synthesize it from the input.
>
>
> On Sat, Feb 19, 2011 at 5:51 AM, Martin Matula <martin.matula_at_oracle.com>wrote:
>
>> Hi,
>> You can use Long instead of Integer which will give you a wider range, but
>> I guess is still not what you want.
>> Other option is to write your own StringReader (StringReaders are used by
>> Jersey to convert QueryParams to a particular type).
>> Regards,
>> Martin
>>
>> On Feb 18, 2011, at 9:21 PM, viggo.navarsete_at_gmail.com wrote:
>>
>> > I have REST endpoint defined like this:
>> > {code}
>> > @GET
>> > @Path("MyEndpoint")
>> > public Response executeMyEndpoint(
>> > ...
>> > @javax.ws.rs.QueryParam("pageSize") Integer pageSize,
>> > @javax.ws.rs.QueryParam("pageNumber") Integer
>> > pageNumber,
>> > ...) {
>> > if( pageNumber != null && ( pageNumber < 1 ||
>> > pageNumber > Integer.MAX_VALUE / pageSize ) ) {
>> > return Response.status( Status.BAD_REQUEST
>> > ).type( MediaType.TEXT_PLAIN )
>> > .entity( "Page number must be
>> > in the interval [1, " + Integer.MAX_VALUE + " / pageSize] (including
>> > endpoints) or omitted." )
>> > .build();
>> > }
>> > ...my code...
>> > }
>> > {code}
>> >
>> > It means that I put some additional restriction on the values of
>> > {{pageNumber}} (except the restriction to {{Integer}} type). If user
>> > passes value which is not within these boundaries, the REST endpoint
>> > returns the error message I specified. However, if it's not even within
>> > Integer datatype range, the endpoint returns always HTTP 404 error
>> > saying "description The requested resource () is not available", which
>> > is not too user friendly. Is there better option how to handle these
>> > corner cases than define pageNumber and pageSize as String and do the
>> > parsing myself?
>>
>>
>
>
> --
> Mark
>