JAVA RESTful WebService实战笔记(四)

前言

在JAVA RESTful WebService实战笔记(三)中已经完成了对JAX-RS2定义的4中过滤器的讲述学习,以下就来看看如何综合运用过滤器,完成一个记录REST请求的访问日志

访问日志(最新版没有AirLogFilter,应该是LoggingFilter)

  • LoggingFilter实现了上述的4个过滤器,记录服务器端和客户端的请求和响应运行时候的信息,LoggingFilter类的定义如下所示:
public final class LoggingFilter implements ContainerRequestFilter, ClientRequestFilter, ContainerResponseFilter,
                                            ClientResponseFilter, WriterInterceptor {
  
  • 1
  • 2
  • LoggingFilter为每一种过滤器接口定义的filter()方法提供了实现,并且也实现了写入的拦截器。在客户端请求过滤中,输出请求资源地址信息和请求投信息;在容器请求过滤中,输出请求方法,请求资源地址信息和请求头信息;在容器响应过滤中,输出HTTP状态码和请求头信息;在客户端响应过滤中,输出HTTP状态码和请求头信息,4个阶段的filter()示例代码如下:
 @Override
    public void filter(final ClientRequestContext context) throws IOException {
        final long id = _id.incrementAndGet();
        context.setProperty(LOGGING_ID_PROPERTY, id);

        final StringBuilder b = new StringBuilder();
        //获取请求方法和地址
        printRequestLine(b, "Sending client request", id, context.getMethod(), context.getUri());
        //获取请求头信息
        printPrefixedHeaders(b, id, REQUEST_PREFIX, context.getStringHeaders());

        if (printEntity && context.hasEntity()) {
            final OutputStream stream = new LoggingStream(b, context.getEntityStream());
            context.setEntityStream(stream);
            context.setProperty(ENTITY_LOGGER_PROPERTY, stream);
            // not calling log(b) here - it will be called by the interceptor
        } else {
            log(b);
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
 @Override
    public void filter(final ClientRequestContext requestContext, final ClientResponseContext responseContext)
            throws IOException {
        final Object requestId = requestContext.getProperty(LOGGING_ID_PROPERTY);
        final long id = requestId != null ? (Long) requestId : _id.incrementAndGet();

        final StringBuilder b = new StringBuilder();
        //获取容器响应状态
        printResponseLine(b, "Client response received", id, responseContext.getStatus());
        //获取容器响应头信息
        printPrefixedHeaders(b, id, RESPONSE_PREFIX, responseContext.getHeaders());
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 单元测试类
public class TIResourceJtfTest extends JerseyTest {
    @Override
    protected Application configure() {
        ResourceConfig config = new ResourceConfig(BookResource.class);
        return config.register(com.example.filter.log.AirLogFilter.class);
    }
    @Override
    protected void configureClient(ClientConfig config) {
        config.register(new AirLogFilter());
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

上述代码中,为了访问日志生效,需要测试类TIResourceJtfTest在Jersey测试框架的服务器端和客户端,分别注册服务日志类AirLogFilter,单元测试结果如下

2017-09-13 10:01:58,135 DEBUG [com.example.resource.TIResourceJtfTest] main - >>Test Post
2017-09-13 10:01:58,232 INFO  [com.example.filter.log.AirLogFilter] main - 1 * AirLog - Request received on thread main
1 / POST http://localhost:9998/books/
1 / Accept: application/json
1 / Content-Type: application/json

2017-09-13 10:01:59,038 DEBUG [com.example.resource.interceptor.AirReaderWriterInterceptor] grizzly-http-server-0 - null:Java Restful Web Service实战-602865027284019:null
2017-09-13 10:01:59,043 DEBUG [com.example.resource.interceptor.AirReaderWriterInterceptor] grizzly-http-server-0 - 602865932131718:Java Restful Web Service实战-602865027284019:null
2017-09-13 10:01:59,084 INFO  [com.example.filter.log.AirLogFilter] main - 2 * AirLog - Response received on thread main
2 \ 200
2 \ Content-Length: 86
2 \ Date: Wed, 13 Sep 2017 02:01:59 GMT
2 \ Content-Type: application/json

2017-09-13 10:01:59,096 DEBUG [com.example.resource.TIResourceJtfTest] main - <<Test Post
九月 13, 2017 10:01:59 上午 org.glassfish.grizzly.http.server
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值