如何将一个对象注入针织衫请求上下文? [英] How to inject an object into jersey request context?

查看:120
本文介绍了如何将一个对象注入针织衫请求上下文?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的场景,我想写一个过滤器,我希望这个过滤器将一些对象插入到当前请求中,并传递给它,以便当资源类获取请求时,它可以使用该对象。



过滤器类

  @Override 
public void filter(ContainerRequestContext request) throws IOException {
MyObject obj = new MyObject();
// Inject MyObject请求我不知道如何
}

资源类别

  @PUT @Consumes(application / json)
@Path(/)
public String create(
JSONParam sample,
@Context MyObject obj){

System.out.println(obj.getName());

return;
}


解决方案

你可以使用 ContainterRequestContext.setProperty(String,Object)。然后只需注入 ContainerRequestContext

  @Override 
public void filter(ContainerRequestContext crc)throws IOException {
MyObject obj = new MyObject();
crc.setProperty(myObject,myObject);
}

@POST
public Response getResponse(@Context ContainerRequestContext crc){
return Response.ok(crc.getProperty(myObject))。build );
}

另一个注入 MyObject 直接是使用HK2功能泽西2提供。



创建一个工厂注入 ContainerRequestContext 并返回 MyObject 。例如

  import javax.inject.Inject; 
import javax.ws.rs.container.ContainerRequestContext;
import jetty.plugin.test.domain.MyObject;
import org.glassfish.hk2.api.Factory;

public class MyObjectFactory实现Factory< MyObject> {

private final ContainerRequestContext context;

@Inject
public MyObjectFactory(ContainerRequestContext context){
this.context = context;
}

@Override
public MyObject provide(){
return(MyObject)context.getProperty(myObject);
}

@Override
public void dispose(MyObject t){}
}

然后您需要绑定工厂:

  public class InjectApplication extends ResourceConfig { 

public InjectApplication(){
...
注册(新的AbstractBinder(){
@Override
protected void configure(){
bindFactory(MyObjectFactory.class)
.to(MyObject.class)
.in(RequestScoped.class);
}
});
}
}

与过滤器中的属性设置相同上面的例子,然后可以使用 @Context


$ b注入 MyObject $ b

  @GET 
public Response getTest(@Context MyObject myObject){
return Response.ok(myObject.getMessage())。build();
}











更新



有关此实施的问题,请参阅此问题



另请参见




I have this scenario where I want to write a filter and I want this filter to insert some object into the current request and pass it on so that when the resource class gets the request it can use the object.

Filter class

@Override
public void filter(ContainerRequestContext request) throws IOException {
    MyObject obj = new MyObject();
    // Inject MyObject to request which I dont know how
}

Resource Class

@PUT @Consumes("application/json")
@Path("/")
public String create(
        JSONParam sample,
        @Context MyObject obj) {

    System.out.println(obj.getName());

    return "";
}

解决方案

You could just use ContainterRequestContext.setProperty(String, Object). Then just inject the ContainerRequestContext

@Override
public void filter(ContainerRequestContext crc) throws IOException {
    MyObject obj = new MyObject();
    crc.setProperty("myObject", myObject);
}

@POST
public Response getResponse(@Context ContainerRequestContext crc) {
    return Response.ok(crc.getProperty("myObject")).build();
}

Another option to inject the MyObject directly is to use the HK2 functionality Jersey 2 offers.

Create a factory the inject the ContainerRequestContext and return the MyObject. For example

import javax.inject.Inject;
import javax.ws.rs.container.ContainerRequestContext;
import jetty.plugin.test.domain.MyObject;
import org.glassfish.hk2.api.Factory;

public class MyObjectFactory implements Factory<MyObject> {

    private final ContainerRequestContext context;

    @Inject
    public MyObjectFactory(ContainerRequestContext context) {
        this.context = context;
    }

    @Override
    public MyObject provide() {
        return (MyObject)context.getProperty("myObject");
    }

    @Override
    public void dispose(MyObject t) {}  
}

You then need to bind the factory:

public class InjectApplication extends ResourceConfig {

    public InjectApplication() {
        ...
        register(new AbstractBinder(){
            @Override
            protected void configure() {
                bindFactory(MyObjectFactory.class)
                        .to(MyObject.class)
                        .in(RequestScoped.class);
            } 
        });
    }
}

With the same setting of the property as in the filter example above, you can then just inject the MyObject with the @Context

@GET
public Response getTest(@Context MyObject myObject) {
    return Response.ok(myObject.getMessage()).build();
}



UPDATE

Please see this question for a problem with this implementation.

See Also:

这篇关于如何将一个对象注入针织衫请求上下文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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