Jersey - 用于注入的 @Context 注释。它是如何运作的?

发布于 2024-11-09 18:16:53 字数 681 浏览 0 评论 0原文

我正在查看一个很好的使用 Jersey 的 REST 教程。 在页面下方,有一个名为 TodoResource 的 Web 资源,它本身包含两个实例变量,

public class TodoResource {
    @Context
    UriInfo uriInfo;

    @Context
    Request request;

    String id;

    public TodoResource(UriInfo uriInfo, Request request, String id) {
        this.uriInfo = uriInfo;
        this.request = request;
        this.id = id;
    }
}

我想知道 UriInfoRequest 实例变量初始化了吗?我知道使用 @Context 注释允许注入信息,但是什么时候会发生这种情况? Jersey 会自动处理吗?

I was looking at a good REST tutorial using Jersey.
Down the page, there is a web resource that is built which is entitled TodoResource which itself contains two instance variables

public class TodoResource {
    @Context
    UriInfo uriInfo;

    @Context
    Request request;

    String id;

    public TodoResource(UriInfo uriInfo, Request request, String id) {
        this.uriInfo = uriInfo;
        this.request = request;
        this.id = id;
    }
}

I was wondering exactly how the UriInfo and Request instance variables are initialized? I know that using the @Context annotation allows for information to be injected, but at what point does this happen? Will this be handled automatically by Jersey?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

作死小能手 2024-11-16 18:16:53

我遇到了一些有趣的结果 注射规则,这是我发现的内容:

public class TodoResource{
  @Context
  UriInfo uriInfo; // Set second
  public TodoResource(@Context UriInfo value){
    uriInfo = value; // Set first (makes sense)
  }
  @Context
  public void setUriInfo(UriInfo value){
    uriInfo = value; // Set third
  }
}

我希望这会有所帮助。

I've run into some interesting results with the Rules of Injection, here's what I've found:

public class TodoResource{
  @Context
  UriInfo uriInfo; // Set second
  public TodoResource(@Context UriInfo value){
    uriInfo = value; // Set first (makes sense)
  }
  @Context
  public void setUriInfo(UriInfo value){
    uriInfo = value; // Set third
  }
}

I hope this helps.

流星番茄 2024-11-16 18:16:53

Jersey 不会修改该类,但它会根据客户端的每个请求创建它。

调用类构造函数后,将注入上下文字段。
(如果您尝试访问构造函数内的这些字段,它们将为 null

在您的情况下,该类不需要特定的构造函数,因此只需:

public TodoResource () {
    // in most cases the ctor stays empty.
    // don't do much work here, remember: the ctor is invoked at every client request
}

但在方法内部(代表网络资源)用 @POST, @GET, ... 注释,您将有权访问上下文字段。

Jersey doesn't modify the class, but it creates it on every request from the client.

After the class constructor was invoked, the context fields are injected.
(Should you try to access those fields inside the constructor, they will be null)

In your case, the class wouldn't need a specific constructor, so just:

public TodoResource () {
    // in most cases the ctor stays empty.
    // don't do much work here, remember: the ctor is invoked at every client request
}

But inside methods (which represent web-resources) annotated with @POST, @GET, ... you would have access to context fields.

2024-11-16 18:16:53

使用@PostConstruct方法注释:

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

@Path("foo")
public class AuthResource {
    @Context
    HttpServletRequest request;

    public AuthResource() {
        //request is null
    }

    @PostConstruct
    public void postConstruct() {
        //request is NOT null
    }

    @PreDestroy
    public void preDestroy() {
       //after rest method executing
    }
}

Use @PostConstruct method annotation:

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

@Path("foo")
public class AuthResource {
    @Context
    HttpServletRequest request;

    public AuthResource() {
        //request is null
    }

    @PostConstruct
    public void postConstruct() {
        //request is NOT null
    }

    @PreDestroy
    public void preDestroy() {
       //after rest method executing
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文