我将一个bean注入到我的REST Web服务中,然后从我的bean调用一个方法,其中抛出以下异常:
throw new WebApplicationException(Response.status(Response.Status.NOT_FOUND)).build());
打印孔堆迹线。我该如何避免打印?
答案 0 :(得分:0)
旧问题,但也许有人仍然需要答案:
您可以为此使用ExceptionMapper。这就是我们的用途:
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 chex@chex.at}
* @author Jakob Galbavy {@literal jg@chex.at}
* @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();
}
}