在实现中设置JAX-RS响应头而不在接口中暴露HttpServletResponse [英] Set JAX-RS response headers in implementation without exposing HttpServletResponse in interface

查看:129
本文介绍了在实现中设置JAX-RS响应头而不在接口中暴露HttpServletResponse的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个RESTful服务器实现以及一个用于调用客户端的库,所有这些都使用JAX-RS。服务器组件分为接口 FooResource 和实现 FooResourceService

I have a RESTful server implementation as well as a library for clients to make the calls, all using JAX-RS. The server components are divided up into interface FooResource and implementation FooResourceService.

为了让客户端和服务器库共享RESTful路径和其他定义,我想将 FooResource 接口拆分为自己的项目:

In order for the client and server libraries to share RESTful path and other definitions, I wanted to split out the FooResource interface into its own project:

@Path(value = "foo")
public interface FooResource {

  @GET
  public Bar getBar(@PathParam(value = "{id}") int id) {

I想在响应中设置一些标题。一种简单的方法是在方法签名中使用 @Context HttpServletResponse

I want to set some headers in the response. One easy way to do this is to use @Context HttpServletResponse in the method signature:

  public Bar getBar(@PathParam(value = "{id}") int id, @Context HttpServletResponse servletResponse) {

但问题是这暴露了界面中的实现细节。更具体地说,它突然需要我的REST定义项目(在客户端和服务器库之间共享)来引入 javax.servlet-api 依赖 - 客户端的东西没有必要(或希望)。

But the problem is that this exposes implementation details in the interface. More specifically, it suddenly requires my REST definition project (which is shared between the client and server library) to pull in the javax.servlet-api dependency---something the client has no need up (or desire for).

我的RESTful资源服务实现如何设置HTTP响应头而不在资源接口中引入该依赖?

我看到一个帖子建议我注入HttpServletResponse作为类成员。但是,如果我的资源服务实现是单例,这将如何工作?是否使用某种代理与线程本地或某些东西来确定正确的servlet响应,即使多个线程同时使用单例类?还有其他解决方案吗?

I saw one post recommending I inject the HttpServletResponse as a class member. But how would this work if my resource service implementation is a singleton? Does it use some sort of proxy with thread locals or something that figures out the correct servlet response even though the singleton class is used simultaneously by multiple threads? Are there any other solutions?

推荐答案

正确答案似乎是注入 HttpServletResponse 在实现的成员变量中,正如我注意到另一篇文章所示。

The correct answer seems to be to inject an HttpServletResponse in the member variable of the implementation, as I noted that another post had indicated.

@Context  //injected response proxy supporting multiple threads
private HttpServletResponse servletResponse;

尽管peeskillet表示泽西岛的半官方列表未列出 HttpServletResponse 作为代理能力类型之一,当我追踪代码时,至少RESTEasy似乎正在创建代理( org.jboss.resteasy.core.ContextParameterInjector $ GenericDelegatingProxy @ XXXXXXXX )。所以据我所知,单线成员变量的线程安全注入似乎正在发生。

Even though peeskillet indicated that the semi-official list for Jersey doesn't list HttpServletResponse as one of the proxy-able types, when I traced through the code at least RESTEasy seems to be creating a proxy (org.jboss.resteasy.core.ContextParameterInjector$GenericDelegatingProxy@xxxxxxxx). So as far as I can tell, thread-safe injection of a singleton member variable seems to be occurring.

这篇关于在实现中设置JAX-RS响应头而不在接口中暴露HttpServletResponse的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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