TAGS :Viewed: 2 - Published at: a few seconds ago

[ Override HttpServletResponse sendError or setStatus ]

I'm working on a project where we are migrating code from Jetty to Jboss. This code is used as an API where different services is calling the service, the service perform the operation and returns the data, but if something goes wrong during the process exceptions are thrown and finally caught in the top level class.

When using Jetty this worked good, hence the Jetty servlet engine added the message sent to the HttpServletResponse sendError function to the status line. But in Jboss, the default status is instead added:

Jetty: return: 500: My Custom error

Jboss: 500: Internal Server Error

I've not been able to add the *USE_CUSTOM_STATUS_MSG_IN_HEADER* to the Jboss, so I thought I might customize the response by creating a HttpServletResponseWrapper and overriding either the sendError or setStatus message to do what I need and return the content in a way I need it.

But, I'm unsure on how to perform this, or if there are any better solutions.

best, Henrik

Answer 1


To wrap the HttpServletResponse, you'll want to create a Filter that creates the HttpServletResponseWrapper and passes it down the chain instead of the original response passed to doFilter(..):

@Override
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) {
    HttpServletResponseWrapper wrapper = new MyHttpServletResponseWrapper(resp);
    chain.doFilter(req, wrapper);
}

private static class MyHttpServletResponseWrapper extends HttpServletResponseWrapper {
    ...
}