users@glassfish.java.net

RE: Jersey, can't set custom status message

From: Andreas Junius <AJunius_at_internode.com.au>
Date: Sun, 28 Oct 2012 22:01:33 +0000

Hi,

I'm using GlassFish Server Open Source Edition 3.1.2.2 (build 5) and
the jersey core jar says it's version 1.11.1

Andy

-----Original Message-----
From: Marek Potociar [mailto:marek.potociar_at_oracle.com]
Sent: Wednesday, 24 October 2012 8:06 PM
To: users_at_glassfish.java.net
Subject: Re: Jersey, can't set custom status message

What version of GlassFish/Jersey are we talking about?

In general, I'm vaguely aware of a related Jersey bug that we solved several months back, but cannot recall the details anymore. Also, sometimes the I/O container under Jersey or the client connector do not respect the custom HTTP phrase, so it's essential to know your full configuration to be able to resolve the issue.

Marek

P.S. Jersey specific questions are more suitable for Jersey user forum - users_at_jersey.java.net ;)

On Oct 24, 2012, at 7:05 AM, Andreas Junius <AJunius_at_internode.com.au> wrote:

> Hi All,
>
> I try to set a custom reason phrase (a status message) using REST Jersey. The following code should do:
>
>
> ...
>
> Response.StatusType status = new Response.StatusType () {
>
> private int code = 412;
> private String msg = "a custom message";
>
> public Family getFamily () {
> return Response.Status.Family.INFORMATIONAL;
> }
>
> public String getReasonPhrase () {
> return msg;
> }
>
> public int getStatusCode () {
> return code;
> }
>
> };
>
> System.out.println("prepare response " + status + " class " + status.getClass());
>
> return Response.status(status).entity("something went wrong").build();
>
>
>
> Unfortunately this does not work at all. It sets the code but not the message. I've had a look at the code for the javax.ws.rs.core.Response class:
>
> ...
>
> /**
> * Set the status on the ResponseBuilder.
> *
> * @param status the response status
> * @return the updated ResponseBuilder
> * @throws IllegalArgumentException if status is less than 100 or greater
> * than 599.
> */
> public abstract ResponseBuilder status(int status);
>
> /**
> * Set the status on the ResponseBuilder.
> *
> * @param status the response status
> * @return the updated ResponseBuilder
> * @throws IllegalArgumentException if status is null
> */
> public ResponseBuilder status(StatusType status) {
> if (status == null)
> throw new IllegalArgumentException();
> return status(status.getStatusCode());
> };
>
> /**
> * Set the status on the ResponseBuilder.
> *
> * @param status the response status
> * @return the updated ResponseBuilder
> * @throws IllegalArgumentException if status is null
> */
> public ResponseBuilder status(Status status) {
> return status((StatusType)status);
> };
>
> ...
>
> As one can see, the status instance given as an argument will never make it into the response object. The class is always using the internal Status enum type.
>
> So, my question is: did anyone find a workaround for the given problem?
>
>
> Cheers,
> Andy
>
>