栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用JAX-RS将请求和响应记录在一处

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用JAX-RS将请求和响应记录在一处

您可以创建过滤器并将其轻松绑定到需要记录的端点,从而使端点保持精简并专注于业务逻辑。

定义名称绑定注释

要将过滤器绑定到REST端点,JAX-
RS提供了元注释

@NameBinding
,它可以按以下方式使用:

@NameBinding@Retention(RUNTIME)@Target({TYPE, METHOD})public @interface Logged { }

记录HTTP请求

@Logged
注释将被用来装饰一个过滤器类,它实现
ContainerRequestFilter
,让您在处理请求:

@Logged@Providerpublic class RequestLoggingFilter implements ContainerRequestFilter {    @Override    public void filter(ContainerRequestContext requestContext) throws IOException {        // Use the ContainerRequestContext to extract information from the HTTP request        // Information such as the URI, headers and HTTP entity are available    }}

@Provider
注释标记的扩展接口,应该由JAX-
RS是可发现的实现过程中提供扫描相运行时。

ContainerRequestContext
帮助你提取HTTP请求信息。

以下是

ContainerRequestContext
API中从HTTP请求获取信息的方法,这些信息可能对您的日志有用:

  • ContainerRequestContext#getMethod()
    :从请求中获取HTTP方法。
  • ContainerRequestContext#getUriInfo()
    :从HTTP请求获取URI信息。
  • ContainerRequestContext#getHeaders()
    :从HTTP请求获取标头。
  • ContainerRequestContext#getMediaType()
    :获取实体的媒体类型。
  • ContainerRequestContext#getEntityStream()
    :获取实体输入流。

记录HTTP响应

要记录响应,请考虑实施

ContainerResponseFilter

@Logged@Providerpublic class ResponseLoggingFilter implements ContainerResponseFilter {    @Override    public void filter(ContainerRequestContext requestContext,  ContainerResponseContext responseContext) throws IOException {        // Use the ContainerRequestContext to extract information from the HTTP request        // Use the ContainerResponseContext to extract information from the HTTP response    }}

ContainerResponseContext
帮助你提取HTTP响应信息。

以下是

ContainerResponseContext
API的一些方法,这些方法可从HTTP响应中获取对您的日志有用的信息:

  • ContainerResponseContext#getStatus()
    :从HTTP响应中获取状态代码。
  • ContainerResponseContext#getHeaders()
    :从HTTP响应中获取标头。
  • ContainerResponseContext#getEntityStream()
    :获取实体输出流。

将过滤器绑定到端点

要将过滤器绑定到端点方法或类,请使用

@Logged
上面定义的注释对它们进行 注释。对于带有注释的方法和/或类,将执行过滤器:

@Path("/")public class MyEndpoint {    @GET    @Path("{id}")    @Produces("application/json")    public Response myMethod(@PathParam("id") Long id) {        // This method is not annotated with @Logged        // The logging filters won't be executed when invoking this method        ...    }    @DELETE    @Logged    @Path("{id}")    @Produces("application/json")    public Response myLoggedMethod(@PathParam("id") Long id) {        // This method is annotated with @Logged        // The request logging filter will be executed before invoking this method        // The response logging filter will be executed before invoking this method        ...    }}

在上面的示例中,日志记录过滤器将仅针对进行执行,

myLoggedMethod(Long)
因为它带有注释
@Logged

附加信息

除了

ContainerRequestContext
ContainerResponseFilter
接口中可用的方法外,您还可以
ResourceInfo
使用
@Context
以下方法插入过滤器:

@ContextResourceInfo resourceInfo;

它可以用来获取

Method
Class
匹配请求的URL其中:

Class<?> resourceClass = resourceInfo.getResourceClass();Method resourceMethod = resourceInfo.getResourceMethod();

HttpServletRequest
并且
HttpServletResponse
也可用于注射:

@ContextHttpServletRequest httpServletRequest;@ContextHttpServletResponse httpServletResponse;


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/486784.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号