gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 09:19:00 24 4
gpt4 key购买 nike

有关 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com