注入Jersey资源类 [英] Injecting into a Jersey Resource class

查看:258
本文介绍了注入Jersey资源类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确实尝试了以下链接
如何将协作者连接到泽西岛资源?

访问Jersey资源类中的外部对象
但是我仍然无法找到一个显示如何注入Resource类的工作示例。
我没有使用Spring或网络容器。

I did try going through the following links How to wire in a collaborator into a Jersey resource? and Access external objects in Jersey Resource class But still i am unable to find a working sample which shows how to inject into a Resource class. I am not using Spring or a web container.

我的资源是

package resource;

import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

@Path("/something")
public class Resource
{
    @MyResource
    Integer foo = null;
    private static String response = "SampleData from Resource";

    public Resource()
    {
        System.out.println("...constructor called :" + foo);
    }

    @Path("/that")
    @GET
    @Produces("text/plain")
    public String sendResponse()
    {
        return response + "\n";
    }
}

我的提供商

package resource;

import javax.ws.rs.ext.Provider;
import com.sun.jersey.core.spi.component.ComponentContext;
import com.sun.jersey.core.spi.component.ComponentScope;
import com.sun.jersey.spi.inject.Injectable;
import com.sun.jersey.spi.inject.InjectableProvider;

@Provider
public class MyResourceProvider implements InjectableProvider<MyResource, Integer>
{
    @Override
    public ComponentScope getScope()
    {
       return ComponentScope.PerRequest;
    }

     @Override
    public Injectable getInjectable(final ComponentContext arg0, final MyResource arg1, final Integer arg2)
    {
       return new Injectable<Object>()
        {
            @Override
            public Object getValue()
            {
              return new Integer(99);
            }
        };
    }
}

我的EndpointPublisher

import java.util.HashMap;
import java.util.Map;
import javax.ws.rs.core.MediaType;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.container.grizzly.GrizzlyWebContainerFactory;

class EndpointPublisher
{
    public static void main(final String[] args)
    {

        final String address = "http://localhost:8080/";
        final Map<String, String> config = new HashMap<String, String>();
        config.put("com.sun.jersey.config.property.packages", "resource");
        try
        {
            GrizzlyWebContainerFactory.create(address, config);
            System.out.println("server started ....." + address);
            callGet();
        }
        catch (final Exception e)
        {
            e.printStackTrace();
        }
    }

    public static void callGet()
    {
        Client client = null;
        ClientResponse response = null;
        client = Client.create();
        final WebResource resource =
                client.resource("http://localhost:8080/something");
        response = resource.path("that")
                .accept(MediaType.TEXT_XML_TYPE, MediaType.APPLICATION_XML_TYPE)
                .type(MediaType.TEXT_XML)
                .get(ClientResponse.class);
        System.out.println(">>>> " + response.getResponseDate());
    }
}

我的注释

@Retention(RetentionPolicy.RUNTIME)
public @interface MyResource
{}

但是当我执行我的EndpointPublisher时,我无法注入foo !!

But when i execute my EndpointPublisher i am unable to inject foo!!

推荐答案

您的InjectableProvider未正确实现。第二个类型参数不应该是您尝试注入的字段的类型 - 而应该是上下文 - java.lang.reflect.Type类或com.sun.jersey.api.model.Parameter类。在您的情况下,您将使用类型。因此,您的InjectableProvider实现应如下所示:

Your InjectableProvider is not implemented correctly. The second type parameter should not be the type of the field you are trying to inject - instead it should be the context - either java.lang.reflect.Type class or com.sun.jersey.api.model.Parameter class. In your case, you would use Type. So, your InjectableProvider implementation should look as follows:

package resource;

import javax.ws.rs.ext.Provider;
import com.sun.jersey.core.spi.component.ComponentContext;
import com.sun.jersey.core.spi.component.ComponentScope;
import com.sun.jersey.spi.inject.Injectable;
import com.sun.jersey.spi.inject.InjectableProvider;
import java.lang.reflect.Type;

@Provider
public class MyResourceProvider implements InjectableProvider<MyResource, Type> {

    @Override
    public ComponentScope getScope() {
        return ComponentScope.PerRequest;
    }

    @Override
    public Injectable getInjectable(final ComponentContext arg0, final MyResource arg1, final Type arg2) {
        if (Integer.class.equals(arg2)) {
            return new Injectable<Integer>() {

                @Override
                public Integer getValue() {
                    return new Integer(99);
                }
            };
        } else {
            return null;
        }
    }
}

有一个帮助类每请求可注射提供程序(PerRequestTypeInjectableProvider)以及单例可注入提供程序(SingletonTypeInjectableProvider),因此您可以通过继承它来进一步简化它:

There is a helper class for per-request injectable providers (PerRequestTypeInjectableProvider) as well as singleton injectable providers (SingletonTypeInjectableProvider), so you can further simplify it by inheriting from that:

package resource;

import javax.ws.rs.ext.Provider;
import com.sun.jersey.core.spi.component.ComponentContext;
import com.sun.jersey.spi.inject.Injectable;
import com.sun.jersey.spi.inject.PerRequestTypeInjectableProvider;

@Provider
public class MyResourceProvider extends PerRequestTypeInjectableProvider<MyResource, Integer> {
    public MyResourceProvider() {
        super(Integer.class);
    }

    @Override
    public Injectable<Integer> getInjectable(ComponentContext ic, MyResource a) {
        return new Injectable<Integer>() {
            @Override
            public Integer getValue() {
                return new Integer(99);
            }
        };
    }
}

请注意,对于这些辅助类,第二个类型参数是字段的类型。

Note that for these helper classes the second type parameter is the type of the field.

还有一件事 - 在调用构造函数之后注入,因此资源的构造函数仍然是打印出 ...构造函数调用:null ,但是如果你改变你的资源方法以返回foo,你会看到你得到的响应将是99。

And one more thing - the injection happens after the constructor is called, so the constructor of your resource will still print out ...constructor called :null, but if you change your resource method to return foo, you'll see the response you'll get will be 99.

这篇关于注入Jersey资源类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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