JAX-RS,组件可以在请求生命周期中访问哪些信息 [英] JAX-RS, components can access which information through the request life cycle

查看:84
本文介绍了JAX-RS,组件可以在请求生命周期中访问哪些信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JAX-RS中处理请求时,我研究的是整个过程的流程如下;

While going through the processing of a request in JAX-RS, what I studied is that the flow of the whole process is as below;

我知道有不同的组件,它们可实现可插入性,例如我们可以有过滤器,并且可以将它们插入任何资源方法 我们想要的是拦截器(我知道它们是围绕MessageBodyWriters和MessageBodyReaders触发的).但是我对每个组件都感到困惑 以及每个组件在请求通过它们时可以访问的信息,

I know that there are different components , which serve the purpose of pluggability, like we can have filters and can plug them to whatever resource methods we want, same is the case with interceptors(and I know that they are triggered around MessageBodyWriters and MessageBodyReaders). But I am confused about each of the component and the piece of information which each component can access while the request is passing through them,

我学到的关键是:

只能修改(或应处理)标头.但是,我在过滤器的ContainerRequestContext中看到了方法getEntityStream.此方法也指向哪个流?

Can modify(or should deal with ) headers only. However I have seen a method getEntityStream in the ContainerRequestContext of filter. Which stream this method is referring too?

我唯一的例子是通过Internet将流压缩到某些GzipReader(或类似的东西)中.它是拦截器的唯一用途吗?而且我怀疑整个请求正文在拦截器中都可用,请参见下图.

Well the only example I had was zipping the stream into some GzipReader(or something like that) over the internet . Is it the only utilisation of the Interceptor? And I suspect that the whole request body is available in the interceptor, see the image below.

它们是可以理解的,我想它们具有与拦截器相同的请求信息级别.

They are understandable, and I guess they have the same level of request information as available to that of the Interceptors.

我还不了解每个组件都可以从传入请求中访问哪些内容,以及每个组件应进行哪些修改,所以这个问题可能很广泛,但专门 指向查询解决方案的链接可能会有所帮助.

I have not understood that what things each component can access from the incoming request, and what should be modified in each of the components, well this question may be broad , but dedicated link pointing to solution of the queries may be helpful.

刚刚测试.过滤器可以访问消息正文并可以对其进行修改,拦截器也可以如此. ://

Just tested. Filter can access the message body and can modify it, So do the interceptor. ://

推荐答案

泽西岛文档是学习更多有关JAX-RS API的良好起点:

The Jersey documentation is a good start point to learn more about the JAX-RS API:

看看文档中有关过滤器:

当您想要修改任何请求或响应参数(例如标头)时,可以使用过滤器.例如,您想向每个生成的响应添加响应头X-Powered-By.不用在每种资源方法中添加此标头,而是使用响应过滤器添加此标头.

Filters can be used when you want to modify any request or response parameters like headers. For example you would like to add a response header X-Powered-By to each generated response. Instead of adding this header in each resource method you would use a response filter to add this header.

服务器端和客户端都有过滤器.

There are filters on the server side and the client side.

服务器过滤器:

  • ContainerRequestFilter
  • ContainerResponseFilter

客户端过滤器:

  • ClientRequestFilter
  • ClientResponseFilter

根据过滤器,您将获得请求/响应上下文对象,该对象可以让您修改请求的方法,请求的URI,标头,响应状态代码等.您还可以访问允许以下操作的流:您可以操纵请求/响应有效负载.

According to the filter, you are provided with the request/response context objects that allows you to modify the requested method, the requested URI, the headers, the response status code, etc. And you also can access a stream that allows you to manipulate the request/response payload.

ContainerRequestContext ContainerResponseContext 是在服务器上 ClientRequestContext ClientResponseContext 是在客户端上可用.

While ContainerRequestContext and ContainerResponseContext are available on server, ClientRequestContext and ClientResponseContext are available on client.

看看文档中有关拦截器:

拦截器为服务器和客户端共享一个公共API.过滤器主要用于操纵请求和响应参数,例如HTTP头,URI和/或HTTP方法,而拦截器则用于通过操纵实体输入/输出流来操纵实体.例如,如果您需要对客户端请求的实体进行编码,则可以实现拦截器来为您完成工作.

Interceptors share a common API for the server and the client side. Whereas filters are primarily intended to manipulate request and response parameters like HTTP headers, URIs and/or HTTP methods, interceptors are intended to manipulate entities, via manipulating entity input/output streams. If you for example need to encode entity body of a client request then you could implement an interceptor to do the work for you.

拦截器有两种, ReaderInterceptor .

阅读器拦截器用于操纵入站实体流.这些是来自电线"的流.因此,使用读取器拦截器,您可以在服务器端(从客户端请求中读取实体)上处理请求实体流,并在客户端(从服务器响应中读取实体)上处理响应实体流.

Reader interceptors are used to manipulate inbound entity streams. These are the streams coming from the "wire". So, using a reader interceptor you can manipulate request entity stream on the server side (where an entity is read from the client request) and response entity stream on the client side (where an entity is read from the server response).

写程序拦截器用于将实体写入线路"的情况,这在服务器上表示写出响应实体时在客户端上,在客户端上写请求发送到服务器的请求实体时在客户端上.

Writer interceptors are used for cases where entity is written to the "wire" which on the server means when writing out a response entity and on the client side when writing request entity for a request to be sent out to the server.

在执行消息正文读取器或编写器之前,将执行写入器和读取器拦截器,其主要目的是包装将在消息正文读取器和编写器中使用的实体流.

Writer and reader interceptors are executed before message body readers or writers are executed and their primary intention is to wrap the entity streams that will be used in message body reader and writers.

[...]

在拦截器中,为您提供了上下文对象,因此您可以访问请求/响应有效负载流.另外,您可以访问一个可变的映射,该映射允许您操纵请求/响应标头.

In the interceptors, you are provided with context objects so you can access the request/response payload streams. Additonaly, you can access a mutable map that allows you to manipulate the request/response headers.

ReaderInterceptorContext WriterInterceptorContext 都可用在客户端和服务器上.

ReaderInterceptorContext and WriterInterceptorContext are available both on client and on server.

这篇关于JAX-RS,组件可以在请求生命周期中访问哪些信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆