나는 JAX-RS는 요청 및 응답 정보를 기록 할 필터,이 같은 로그인이 :읽기 JAX-RS 몸의 InputStream 두 번
public class LoggingFilter implements ContainerRequestFilter, ContainerResponseFilter {
@Override
public void filter(final ContainerRequestContext requestContext) throws IOException {
...
String body = getBody(request);
...
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("request: {}", httpRequest);
}
}
}
getBody()
방법은 InputStream
의 본문 내용 읽기를하지만 필요 이 스트림을 다시 설정할 수 없으므로 일부 트릭을 수행하십시오.
private String getBody(final ContainerRequestContext requestContext) {
try {
byte[] body = IOUtils.toByteArray(requestContext.getEntityStream());
InputStream stream = new ByteArrayInputStream(body);
requestContext.setEntityStream(stream);
return new String(body);
} catch (IOException e) {
return null;
}
}
본문 내용을 읽을 수있는 더 좋은 방법이 있나요 :이 작은 트릭없이 내 나머지 방법은 항상 빈 요청 본문 콘텐츠를 수신?
[link] (https://dennis-xlc.gitbooks.io/restful-java-with-jax-rs-2-0-2rd-edition/en/part1/chapter12)의 인터셉터 및 필터 섹션을 참조하십시오. /reader_and_writer_interceptors.html) – Gautam