JAX-RS接口标记和@Context注入
考虑以下简单的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如这个问题的措辞,这可以通过实现中的 @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.