JAX-RS接口标记和@Context注入

发布于 2024-08-12 01:21:34 字数 888 浏览 1 评论 0原文

考虑以下简单的 RESTEasy (JAX-RS) 服务:

@Path("/example-service")
public interface ExampleService {

    @Path("/ping")
    @GET
    public String ping(String message);

}

我想在接口而不是类上定义 JAXRS 细节,这样我就可以使用漂亮的客户端框架,即:

ExampleService client = ProxyFactory.create(ExampleService.class, "http://localhost:8080");

一切都运行良好,除了当我想引入一些 RESTEasy 的时候上下文注入,即:@Context。天真地考虑以下内容:

@Path("/example-service")
public interface ExampleService {

    @Path("/ping")
    @GET
    public String ping(@Context HttpServletRequest request, String message);

}

这显然没有意义,因为这个 @Context 注入是正交的并且不属于接口(此外,即使我可以从客户端角度克服这个接口的丑陋并传递 null,目前存在一个错误,阻止此功能工作:RESTEASY-311

如何我使用接口 JAXRS 标记(因此利用了很好的 RESTEasy 客户端框架)并同时访问正交 @Context 注入?

Consider the following simple RESTEasy (JAX-RS) service:

@Path("/example-service")
public interface ExampleService {

    @Path("/ping")
    @GET
    public String ping(String message);

}

I want to define JAXRS specifics on the interface rather than the class so I can use the nice client framework, ie:

ExampleService client = ProxyFactory.create(ExampleService.class, "http://localhost:8080");

Everything works well, except for when I want to introduce some of RESTEasy's context injections, ie: @Context. Naively, consider the following:

@Path("/example-service")
public interface ExampleService {

    @Path("/ping")
    @GET
    public String ping(@Context HttpServletRequest request, String message);

}

This obviously doesn't make sense because this @Context injection is orthogonal and doesn't belong on the interface (furthermore, even if I can get past the ugliness of this interface from the client perspective and pass null, there is currently a bug preventing this from working: RESTEASY-311)

How can I use interface JAXRS markup (and therefore leverage the nice RESTEasy client framework) and access orthogonal @Context injections at the same time?

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

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

发布评论

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

评论(1

窗影残 2024-08-19 01:21:34

正如这个问题的措辞,这可以通过实现中的 @Context 字段注入轻松解决。使这种特殊情况变得复杂的是使用 RESTEasy 的“ejb-integration”,它从 JNDI 检索实现。该解决方案(尚)不会对从 EJB 容器检索的实现执行额外的 REST 注入。通过即将推出的 JEE6/JAXRS 集成,这一切都将变得更加容易。

As this question is phrased, this can be easily solved with a @Context field injection in the implementation. What complicated this particular situation was the use of RESTEasy's "ejb-integration" which retrieves the implementation from JNDI. This solution does not (yet) perform additional REST injections on the implementation retrieved from the EJB container. This will all be easier with the upcoming JEE6/JAXRS integration.

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