Skip to main content

Jersey, can't set custom status message

3 replies [Last post]
Anonymous

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

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
m_potociar
Offline
Joined: 2006-05-03

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@jersey.java.net ;)

On Oct 24, 2012, at 7:05 AM, Andreas Junius 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
>
>

Andreas Junius

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...]
Sent: Wednesday, 24 October 2012 8:06 PM
To: users@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@jersey.java.net ;)

On Oct 24, 2012, at 7:05 AM, Andreas Junius 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
>
>

Andreas Junius

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

On Wed, Oct 24, 2012 at 8:05 PM, Marek Potociar
wrote:
> 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@jersey.java.net ;)
>
> On Oct 24, 2012, at 7:05 AM, Andreas Junius 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
>>
>>
>