java - JaxRS + RestEasy - 如何创建自己的 @Context 注入(inject)字段?

标签 java jboss jax-rs resteasy

有关 JBoss 7.1.0 上的 RestEASY 3.6.2 的问题。

我有以下可用的 JaxRS 服务:

@Path("my-service")
public class MyResource {
  @Context
  HttpServletRequest request;

  @GET
  @Path("get-stuff")
  @Produces(MediaType.APPLICATION_JSON)
  public Response doStuff() {
    MyCustomContext customContext = new MyCustomContext(request);
    // ... use the customContext here.
  }
}

按照当前的设置方式,每个休息方法都需要一个 MyCustomContext customContext = new MyCustomContext(request);。这很烦人。

有什么方法可以注入(inject)MyCustomContext吗?

@Path("my-service")
public class MyResource {
  @Context
  MyCustomContext context;

  @GET
  @Path("get-stuff")
  @Produces(MediaType.APPLICATION_JSON)
  public Response doStuff() {
    // ... use the customContext here.
  }
}

@Producer // ???
public class MyCustomContext {
  @Context
  HttpServletRequest request;

  public MyCustomContext() {
    // construct with request object.
  }
}

我发现了大量的链接暗示了一种方法,但我一无所获。

最佳答案

我不知道用 @Context 注入(inject)自定义类实例/bean 的任何方法。我想概述取决于具体要求的替代方法。

A) 根本不需要注入(inject)。

使您的自定义上下文成为 JAX-RS 资源类的类成员(而不是每个方法中的局部变量)。利用@PostConstruct一旦容器创建了初始化的资源类实例,就实例化您的自定义上下文。资源类必须是具有请求范围的 CDI-bean,才能使其正常工作。

@Path("my-service")
@RequestScoped // CDI-bean with request scope only
public class MyResource {

  @Context
  private HttpServletRequest request;

  private MyCustomContext customContext;

  @PostConstruct
  public void initialize() {
    this.customContext = new MyCustomContext(this.request); // request is initialized by the container already at this point
  }

  @GET
  @Path("get-stuff")
  @Produces(MediaType.APPLICATION_JSON)
  public Response doStuff() {
    // ... use the customContext here.
  }
}

B) 您的自定义上下文需要 HttpServletRequest仅实例

除了 JAX-RS 之外,通过@Context,CDI also provides a predefined bean通过 @Inject 获取 HttpServletRequest 。您还可以将自定义上下文设为 CDI-bean,并注入(inject)该预定义的 CDI-bean。之后,您可以将自定义上下文注入(inject)到 JAX-RS 资源中(无论它是 EJB 还是 CDI-bean)。

@Dependent // make your custom context a CDI-bean
public class MyCustomContext {

  @Inject // inject predefined CDI-bean
  private HttpServletRequest request;
}
@Path("my-service")
@RequestScoped // either CDI-bean
//@Stateless // or EJB
public class MyResource {

  @Inject // inject custom context via CDI
  private MyCustomContext customContext;

  @GET
  @Path("get-stuff")
  @Produces(MediaType.APPLICATION_JSON)
  public Response doStuff() {
    // ... use the customContext here.
  }
}

C) 您的自定义上下文需要通过 provider specific @Context 提供的唯一实例例如Request

如果您通过 @Context 将实例注入(inject)到非 JAX-RS 自定义上下文 CDI-bean 中,则该实例将为 null。您需要某种机制来从 JAX-RS 资源提供注入(inject)的实例。让 CDI 负责在自定义上下文中通过 @Inject 进行注入(inject),并通过 @Produces 添加生产者方法添加到您的 JAX-RS 资源即可完成这项工作。

@Dependent // make your custom context a CDI-bean
public class MyCustomContext {

  //@Context // in non JAX-RS context the instance will be null
  @Inject // instead inject the JAX-RS context instance via CDI
  private Request request;
}
@Path("my-service")
@RequestScoped // either CDI-bean
//@Stateless // or EJB
public class MyResource {

  @Context // in JAX-RS context the instance will not be null
  private Request request;
  @Inject
  private MyCustomContext customContext;

  @Produces // provide the JAX-RS context instance for injection via CDI
  @RequestScoped
  public Request getContextRequest() {
    return this.request;
  }

  @GET
  @Path("get-stuff")
  @Produces(MediaType.APPLICATION_JSON)
  public Response doStuff() {
    // ... use the customContext here.
  }
}

关于java - JaxRS + RestEasy - 如何创建自己的 @Context 注入(inject)字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58822033/

相关文章:

java - 在 Java 8 中,如何对不带参数并返回 void 的方法进行方法引用?

jboss - "WFLYCTL0412: Required services that are not installed:"=> ["jboss.jdbc-driver.mysql"]

java - jar 文件位置 - Ear 文件还是服务器 lib 文件夹?

java - "JAXWS wsimport"表示 "jaxb file binding"中的 XPath 为空

java - 完成后重新启动 CountDownTimer 比使用按钮重置要慢

java - 扩展 RepositoryRestMvcConfiguration 打破了 Jackson LocalDateTime 序列化

java - 服务器端和客户端使用不同的 WAR 并将它们部署在不同的应用程序服务器中的优点和缺点

java - 不直接编写 Servlet 来创建 REST API 的原因

api - 我应该使用什么规范来记录 REST API?

java - JAX-RS POST 文件输入流为空