Too convoluted and not practical.
If you are sending in a STRING when you're expecting an INT then by definition you have a BAD REQUEST.
A BAD REQUEST is a client problem, not a server side one. Therefore, you should just return status code 400 - Bad Request.
Otherwise, you'll be violating well known practices.
Date: Fri, 15 Apr 2011 17:42:40 +0200
From: daniel.j.larsson_at_gmail.com
To: users_at_jersey.java.net
Subject: [Jersey] Re: How to catch the response in case of an invalid query paramter
I'm not sure what control you'll have of the response, but you can try create a wrapper class for the parameter:
class IntParam {
...
public static IntParam valueOf(String value) {
// Parse string, try throw a WebApplicationException // with the response in case it fails. If I
// remember correctly, the response here is // ignored though :/
... }}
@GET
@Path("get")
public MyClass getSomething ( @QueryParam("age") IntParam age) { ...
}
2011/4/15 Pengfei Di <pengfei.di_at_match2blue.com>
thanks for the suggestions!
Yes, I know that I can circumvent the problem by declaring the age
as String.
However, I really want to know whether it is possible to declare the
age as Integer, and then catch the exception or define the response
by myself.
Pengfei
On 04/15/2011 05:35 AM, Rob - wrote:
yep, 400 Bad request is better.
Date: Fri, 15 Apr 2011 04:12:53 +0200
From: daniel.j.larsson_at_gmail.com
To: users_at_jersey.java.net
Subject: [Jersey] Re: How to catch the response in case of an
invalid query paramter
Or 400 BAD REQUEST, the problem is with the client, not the
server.
2011/4/15 Rob - <skyscrapper666_at_hotmail.com>
I'd declare "age" as a string and handle the validation
myself. If error then return a 500 status code.
No real benefits having "age" declared as integer (to
my eyes).
> Date: Thu, 14 Apr 2011 12:42:57 +0200
> From: pengfei.di_at_match2blue.com
> To: users_at_jersey.java.net
> Subject: [Jersey] How to catch the response in case
of an invalid query paramter
>
> Hello,
>
> I have a very simple GET REST API, which returns
something only based on
> the query parameter. For example:
> @GET
> @Path("get")
> public MyClass getSomething ( @QueryParam("age")
Integer age);
>
> As listed above, I specify the parameter type as
Integer. However,
> problem occurs when the given parameter cannot be
parse to an integer.
> For an instance: if the parameter is set as
"age=one" instead of
> "age=1", Jersey server will say "HTTP status 404
- Not Found."
>
> What I want to do is to specify the response by
myself, but I have no
> idea where I can do it. Shouldn't Jersey throw an
> NumberFormatException. In fact, it doesn't. :(
>
> Thanks for any hint
> Pengfei
>
>
--
Pengfei Di
Technology
match2blue software development GmbH
Leutragraben 1
07743 Jena
Tel: +49 3641 816 8092
Mobil: +49 1520 166 8691
Fax: +49 3641 573 3479
Email: pengfei.di_at_match2blue.com
Web : www.match2blue.com
Blog :
http://blog.match2blue.com
Registergericht: Amtsgericht Jena
Registernummer: HRB 503726
Geschäftsführerin: Stephanie Renda