这是一种可以实现的方法,与Jersey实现其日志记录过滤器的方式非常相似。您可以阅读该实体并将其粘贴回请求,因此您不小心在过滤器中不使用它。
import com.sun.jersey.api.container.ContainerException;import com.sun.jersey.core.util.ReaderWriter;import com.sun.jersey.spi.container.ContainerRequest;import com.sun.jersey.spi.container.ContainerRequestFilter;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;public class JerseyFilter implements ContainerRequestFilter { @Override public ContainerRequest filter(ContainerRequest request) { ByteArrayOutputStream out = new ByteArrayOutputStream(); InputStream in = request.getEntityInputStream(); final StringBuilder b = new StringBuilder(); try { if (in.available() > 0) { ReaderWriter.writeTo(in, out); byte[] requestEntity = out.toByteArray(); printEntity(b, requestEntity); request.setEntityInputStream(new ByteArrayInputStream(requestEntity)); } return request; } catch (IOException ex) { throw new ContainerException(ex); } } private void printEntity(StringBuilder b, byte[] entity) throws IOException { if (entity.length == 0) return; b.append(new String(entity)).append("n"); System.out.println("#### Intercepted Entity ####"); System.out.println(b.toString()); }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)