users@jersey.java.net

[Jersey] Re: Response.getStatus() and code readability

From: Marek Potociar <marek.potociar_at_oracle.com>
Date: Tue, 12 Nov 2013 13:44:14 +0100

On 08 Nov 2013, at 21:44, cowwoc <cowwoc_at_bbs.darktech.org> wrote:

> Hi,
>
> I noticed Jersey 2.0 moved from returning an enum Status to returning an int code.

Jerse 2.0 uses JAX-RS API. JAX-RS Response.getStatus() always returned int, there was no way to change it.

> Can you please explain the reasoning and suggest a way to use switch() statements on response codes while keeping things readable?

Here are the reasons:

1. See above - existing JAX-RS Response API
2. We have received several bug reports in Jersey 1.x that it is not possible to support custom reason phrase. This is indeed impossible with enum.
3. You can still use switch with int.

> In Jersey 1.0 I used to do:
>
> ClientResponse response = ...;
> switch (response.getStatus())
> {
> case OK: doThis(); break;
> case NOT_FOUND: doThat(); break;
> // etc...
> }
>
> and it was quite readable. I am afraid that
>
> Response response = ...;
> switch (response.getStatus())
> {
> case 200: doThis(); break;
> case 404: doThat(); break;
> // etc...
> }
>
> is a lot less readable (requiring me to remember what each code means).

You can do

switch (Response.Status.fromStatusCode(response.getStatus())) { ... }

A little bit more verbose, but should solve your primary concern.

Marek

>
> Thanks,
> Gili