注入 Jersey 资源类
我确实尝试过以下链接如何将合作者连接到泽西资源?一>和访问 Jersey Resource 类中的外部对象但我仍然无法找到一个显示如何注入资源类的工作示例.我没有使用 Spring 或 Web 容器.
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 + "
";
}
}
我的提供者是
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 类.在您的情况下,您将使用 Type.因此,您的 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.
还有一件事——注入发生在在构造函数被调用之后,所以你的资源的构造函数仍然会打印出 ...constructor called :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.
相关文章