users@glassfish.java.net

Re: Jersey, can't set custom status message

From: Marek Potociar <marek.potociar_at_oracle.com>
Date: Wed, 24 Oct 2012 11:35:37 +0200

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
>
>