users@jersey.java.net

[Jersey] How to force empty response on error

From: Vinicius Carvalho <java.vinicius_at_gmail.com>
Date: Thu, 30 Aug 2012 16:13:32 -0400

Hello there! This is bugging me for a couple of hours now.

We are running jersey 1.1.13 inside tomcat.

We don't want to have tomcat exposing those error pages as content body
whenever an error happens. Even using ExceptionMappers I found out that
jersey uses response.sendError instead of response.setStatus at the
WebComponent class:

 if (cResponse.getStatus() >= 400) {

                if (useSetStatusOn404 && cResponse.getStatus() == 404) {

                    response.setStatus(cResponse.getStatus());

                } else {

                    final String reason = cResponse
.getStatusType().getReasonPhrase();

                    if (reason == null || reason.isEmpty()) {

                        response.sendError(cResponse.getStatus());

                    } else {

                        response.sendError(cResponse.getStatus(), reason);

                    }

                }

            } else {

                response.setStatus(cResponse.getStatus());

            }


I really would like to avoid having to configure empty html pages at
<error> section of my web.xml. Isn't there a way to just force the Response
to be empty?


I "fixed" this by making this call inside my Mapper:


return Response.status(Status.NOT_FOUND).type(MediaType.TEXT_PLAIN).entity("
").build();


But it feels so ugly to me, I really would love if:

return Response.status(Status.NOT_FOUND).build();

Could just work with empty response bodies and not default to container
specific error pages.


Any way to fix this?


Regards