gpt4 book ai didi

java - 发出请求时 JAX-RS 'endpoint' 的行为如何?

转载 作者:搜寻专家 更新时间:2023-10-31 08:27:01 24 4
gpt4 key购买 nike

有些事情我不确定我是否理解正确,因此,我需要帮助:)

我看过这个:example ,

@Path("/resource")
public class Resource {
@Context
private HttpHeaders headers;

@GET
public void get(@Context UriInfo uriInfo) {
/* use headers or uriInfo variable here */
}
}

这是否意味着对于每个请求,转换为“端点”的类都会创建一个单独的线程?因为,否则标题信息将不准确...

您能否指出一个(简称:))资源,而不是 JAX-WS 规范,我可以在哪里找到相关信息?

最佳答案

我想不出比 JAX-RS 1.1 spec itself 更短更直接的资源了.清楚what you are asking :

JAX-RS provides facilities for obtaining and processing information about the application deployment context and the context of individual requests. (...)

Context is specific to a particular request (...).

我可以补充一点:上下文信息是通过 @Context 注释获得的。对于资源,上下文信息仅对使用@Path 注释的资源(也称为根资源)可用。此外,@Context 可以注入(inject)以下 context types :ApplicationUriInfoHttpHeadersRequestSecurityContextProviders

关于 lifecycle (请求/线程管理):

3.1.1 Lifecycle and Environment

By default a new resource class instance is created for each requestto that resource. First the constructor is called,then any requested dependencies are injected (context is one of those dependencies), thenthe appropriate method is invoked and finally theobject is made available for garbage collection.

An implementation MAYoffer other resource class lifecycles, mechanisms for specifying theseare outside the scope of this specification. E.g. an implementationbased on an inversion-of-control framework may support all of thelifecycle options provided by that framework.

结论是:

  • 默认情况下,每个请求都由不同的资源实例处理;
  • 上下文是在请求时注入(inject)的(因此每个实例都有不同的上下文)。

每个特定的实现可能会稍微改变这个生命周期,但应该保持原则(每个请求特定的上下文)。

正如您所见,该规范也没有提及线程管理。由于大多数 JAX-RS 实现都是基于 Servlet 的,因此我们可以假设每个请求实例进入不同的线程 - 因为 servlet 容器是每个请求的线程。

关于java - 发出请求时 JAX-RS 'endpoint' 的行为如何?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17044953/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com