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?
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();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With