Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAX RS WebApplicationException do not log

I inject a bean into my REST web service and then call a method from my bean where the following exception gets thrown:

throw new WebApplicationException(Response.status(Response.Status.NOT_FOUND)).build());

The hole stack trace gets printed. How do i avoid the printing?

like image 503
perotom Avatar asked Sep 13 '25 11:09

perotom


1 Answers

Old Question, but maybe someone still needs an answer to this:

You can use an ExceptionMapper for this. Here's, what we use:

package at.chex.archichexture.exceptions;

import javax.inject.Inject;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
import org.slf4j.Logger;

/**
 * @author cheX GmbH Austria {@literal [email protected]}
 * @author Jakob Galbavy {@literal [email protected]}
 * @since 19.06.18
 */
@Provider
public class WebApplicationExceptionMapper implements ExceptionMapper<WebApplicationException> {

  @Inject
  private Logger log;

  @Override
  public Response toResponse(WebApplicationException webApplicationException) {
    int status = webApplicationException.getResponse().getStatus();
    if (status >= 400 && status < 500) {
      log.warn("Returning Error ({}): {}", status, webApplicationException.getLocalizedMessage());
    } else {
      log.error("Exception in Webservice {}", status, webApplicationException);
    }
    return webApplicationException.getResponse();
  }
}
like image 75
Jakob Galbavy Avatar answered Sep 16 '25 11:09

Jakob Galbavy