users@glassfish.java.net

Jersey, can't set custom status message

From: Andreas Junius <AJunius_at_internode.com.au>
Date: Wed, 24 Oct 2012 05:05:18 +0000

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