I\'m looking for a way to log JSON posts in a RESTEASY framework.
I woul like to log the POST body to log file to see what the client is sending to me.
Is t
You can use a ContainerRequestFilter:
@Provider
public class LogFilter implements ContainerRequestFilter {
private Logger LOG = LoggerFactory.getLogger(LogFilter.class);
@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
if (!"POST".equals(requestContext.getMethod())
|| !MediaType.APPLICATION_JSON_TYPE.equals(requestContext.getMediaType())
|| requestContext.getEntityStream() == null) {
return;
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
IOUtils.copy(requestContext.getEntityStream(), baos);
byte[] bytes = baos.toByteArray();
LOG.info("Posted: " + new String(bytes, "UTF-8"));
requestContext.setEntityStream(new ByteArrayInputStream(bytes));
}
}
Instead of checking for Method and Content-Type you can also register this filter per @NameBinding only where you need it.
Note: This simple example copies the InputStream of the request so it will be read twice (maybe a performance problem).