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

问题描述 投票:0回答:1

关于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);。真烦人。

是否有注入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.
  }
}

我发现了很多链接,这些链接暗示着一种实现此目的的方法,但是我实在是空虚的。

java jboss jax-rs resteasy
1个回答
1
投票

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

A)完全不需要注射。

使您的自定义上下文成为您的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实例

[通过@Context在JAX-RS旁边,对于通过CDI also provides a predefined bean的HttpServletRequest为@Inject。您还可以将自定义上下文设置为CDI-bean,然后注入该预定义的CDI-bean。之后,您可以将自定义上下文注入到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提供的实例Exclusiveley,例如Request

如果通过@Context将实例注入到您的非JAX-RS自定义上下文CDI-bean中,它将为null。您需要某种机制来从JAX-RS资源中提供注入的实例。通过您的自定义上下文中的@Inject使CDI负责注入,并通过@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.
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.