Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/hadoop/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Guice Jersey客户端配置MessageBodyWriter和Injection_Jersey_Jax Rs_Guice - Fatal编程技术网

Guice Jersey客户端配置MessageBodyWriter和Injection

Guice Jersey客户端配置MessageBodyWriter和Injection,jersey,jax-rs,guice,Jersey,Jax Rs,Guice,我试图让Guice injection从客户端的角度与Jersey MessageBodyReader/MessageBodyWriter一起工作。我已经用Guice正确启动了服务器。我的问题在于客户 我快速组合了以下内容来演示错误:severy:constructor public FooExample$fooreder(FooExample$foouUnmarshaller)在参数索引0处缺少依赖项 import java.io.IOException; 导入java.io.InputStr

我试图让Guice injection从客户端的角度与Jersey MessageBodyReader/MessageBodyWriter一起工作。我已经用Guice正确启动了服务器。我的问题在于客户

我快速组合了以下内容来演示错误:
severy:constructor public FooExample$fooreder(FooExample$foouUnmarshaller)在参数索引0处缺少依赖项

import java.io.IOException;
导入java.io.InputStream;
导入java.lang.annotation.annotation;
导入java.lang.reflect.Type;
导入javax.inject.inject;
导入javax.ws.rs.WebApplicationException;
导入javax.ws.rs.core.MediaType;
导入javax.ws.rs.core.MultivaluedMap;
导入javax.ws.rs.core.UriBuilder;
导入javax.ws.rs.ext.MessageBodyReader;
导入com.google.inject.AbstractModule;
导入com.google.inject.Guice;
导入com.google.inject.Injector;
导入com.google.inject.Provides;
导入com.sun.jersey.api.client.client;
导入com.sun.jersey.api.client.WebResource;
导入com.sun.jersey.api.client.config.ClientConfig;
导入com.sun.jersey.api.client.config.DefaultClientConfig;
类示例{
公共静态void main(字符串[]args){
Injector i=Guice.createInjector(new FooExample().new FooModule());
WebResource服务=i.getInstance(WebResource.class);
服务路径(“bar”)
.accept(MediaType.APPLICATION\u XML)
.put(String.class,“test123”);
}
公共类FooModule扩展了AbstractModule{
@凌驾
受保护的void configure(){
绑定(fooundmarshaller.class).to(simpleFounMarshaller.class);
}
@提供
公共Web资源配置客户端(){
ClientConfig=newdefaultclientconfig();
config.getClasses().add(FooReader.class);
返回Client.create(config.resource)(
UriBuilder.fromUri(“http://localhost:8080/foo);
}
}
公共静态类Foo{}
公共静态接口解编程序{
公共Foo解组(输入流为);
}
公共静态类SimpleFounMarshaller实现FoouUnmarshaller{
@凌驾
public Foo unmarshall(输入流为){
返回新的Foo();
}
}
公共静态类FooReader实现MessageBodyReader{
私人终审法院;
@注入
公共FooReader(FooUnmarshaller-marshaller){
this.marshaller=marshaller;
}
@凌驾
公共布尔值是可读的(
类类型,
类型genericType,
注释[]注释,
媒体类型(媒体类型){
返回true;
}
@凌驾
公共食物阅读(
类类型,
类型genericType,
注释[]注释,
MediaType MediaType,
多值MAP HttpHeader,
InputStream entityStream)引发IOException、WebApplicationException{
返回封送拆收器(entityStream);
}
}
}
我从中获得控制台输出:

Oct 23, 2012 3:17:22 PM com.sun.jersey.spi.inject.Errors processErrorMessages
SEVERE: The following errors and warnings have been detected with resource and/or provider classes:
  SEVERE: Missing dependency for constructor public FooExample$FooReader(FooExample$FooUnmarshaller) at parameter index 0
Exception in thread "main" com.google.inject.ProvisionException: Guice provision errors:

1) Error in custom provider, com.sun.jersey.spi.inject.Errors$ErrorMessagesException
  at FooExample$FooModule.configuredClient(FooExample.java:40)
  while locating com.sun.jersey.api.client.WebResource

1 error
  at com.google.inject.internal.InjectorImpl$4.get(InjectorImpl.java:987)
  at com.google.inject.internal.InjectorImpl.getInstance(InjectorImpl.java:1013)
  at FooExample.main(FooExample.java:25)
Caused by: com.sun.jersey.spi.inject.Errors$ErrorMessagesException
  at com.sun.jersey.spi.inject.Errors.processErrorMessages(Errors.java:170)
  at com.sun.jersey.spi.inject.Errors.postProcess(Errors.java:136)
  at com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:199)
  at com.sun.jersey.api.client.Client.<init>(Client.java:187)
  at com.sun.jersey.api.client.Client.<init>(Client.java:170)
  at com.sun.jersey.api.client.Client.create(Client.java:679)
  at FooExample$FooModule.configuredClient(FooExample.java:42)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:616)
  at com.google.inject.internal.ProviderMethod.get(ProviderMethod.java:104)
  at com.google.inject.internal.InternalFactoryToProviderAdapter.get(InternalFactoryToProviderAdapter.java:40)
  at com.google.inject.internal.InjectorImpl$4$1.call(InjectorImpl.java:978)
  at com.google.inject.internal.InjectorImpl.callInContext(InjectorImpl.java:1024)
  at com.google.inject.internal.InjectorImpl$4.get(InjectorImpl.java:974)
  ... 2 more
2012年10月23日下午3:17:22 com.sun.jersey.spi.inject.Errors processErrorMessages
严重:在资源和/或提供程序类中检测到以下错误和警告:
严重:在参数索引0处缺少构造函数公共FooExample$FooReader(FooExample$FooUnmarshaller)的依赖项
线程“main”com.google.inject.ProvisionException中出现异常:Guice配置错误:
1) 自定义提供程序com.sun.jersey.spi.inject.Errors$ErrorMessagesException中出错
在FooExample$foodModule.configuredClient(FooExample.java:40)
查找com.sun.jersey.api.client.WebResource时
1错误
位于com.google.inject.internal.InjectorImpl$4.get(InjectorImpl.java:987)
位于com.google.inject.internal.InjectorImpl.getInstance(InjectorImpl.java:1013)
位于FooExample.main(FooExample.java:25)
原因:com.sun.jersey.spi.inject.Errors$ErrorMessagesException
位于com.sun.jersey.spi.inject.Errors.processErrorMessages(Errors.java:170)
位于com.sun.jersey.spi.inject.Errors.postProcess(Errors.java:136)
位于com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:199)
位于com.sun.jersey.api.client.client.(client.java:187)
位于com.sun.jersey.api.client.client.(client.java:170)
位于com.sun.jersey.api.client.client.create(client.java:679)
在FooExample$FooModule.configuredClient(FooExample.java:42)
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处
在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)中
在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)中
位于java.lang.reflect.Method.invoke(Method.java:616)
在com.google.inject.internal.ProviderMethod.get(ProviderMethod.java:104)上
位于com.google.inject.internal.InternalFactoryToProviderAdapter.get(InternalFactoryToProviderAdapter.java:40)
在com.google.inject.internal.InjectorImpl$4$1.call上(InjectorImpl.java:978)
位于com.google.inject.internal.InjectorImpl.callInContext(InjectorImpl.java:1024)
位于com.google.inject.internal.InjectorImpl$4.get(InjectorImpl.java:974)
... 还有两个

我觉得我需要使用
GuiceComponentProviderFactory
,但我似乎找不到任何关于它的文档,也找不到
IoCComponentProviderFactory
ClientConfig
的文档。任何帮助都将不胜感激。谢谢

所以我通过猜测和检查解决了自己的问题。我不能确认这是一种预期的做事方式,但这是可行的

Client
类有一个方法:
publicstaticclient-create(ClientConfig-cc,IoCComponentProviderFactory-provider)
,我将一个
GuiceComponentProviderFactory
传递给它,并解决了问题。上述代码的工作版本为:

import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

import javax.inject.Inject;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.UriBuilder;
import javax.ws.rs.ext.MessageBodyReader;

import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.api.core.DefaultResourceConfig;
import com.sun.jersey.core.spi.component.ioc.IoCComponentProviderFactory;
import com.sun.jersey.guice.spi.container.GuiceComponentProviderFactory;

class FooExample {
  public static void main(String[] args) {
    WebResource service = configuredClient();

    service.path("bar")
        .accept(MediaType.APPLICATION_XML)
        .put(String.class, "test123");
  }

  private static WebResource configuredClient() {
    DefaultClientConfig config = new DefaultClientConfig();
    config.getClasses().add(FooReader.class);
    return Client.create(config, provider()).resource(
        UriBuilder.fromUri("http://localhost:8080/foo").build());
  }

  private static IoCComponentProviderFactory provider() {
    return new GuiceComponentProviderFactory(
        new DefaultResourceConfig(),
        Guice.createInjector(new FooExample().new FooModule()));
  }

  public class FooModule extends AbstractModule {
    @Override
    protected void configure() {
      bind(FooUnmarshaller.class).to(SimpleFooUnmarshaller.class);
    }
  }

  public static class Foo {}

  public static interface FooUnmarshaller {
    public Foo unmarshall(InputStream is);
  }

  public static class SimpleFooUnmarshaller implements FooUnmarshaller {
    @Override
    public Foo unmarshall(InputStream is) {
      return new Foo();
    }
  }

  public static class FooReader implements MessageBodyReader<Foo> {
    private final FooUnmarshaller marshaller;

    @Inject
    public FooReader(FooUnmarshaller marshaller) {
      this.marshaller = marshaller;
    }

    @Override
    public boolean isReadable(
        Class<?> type,
        Type genericType,
        Annotation[] annotations,
        MediaType mediaType) {
      return true;
    }

    @Override
    public Foo readFrom(
        Class<Foo> type,
        Type genericType,
        Annotation[] annotations,
        MediaType mediaType,
        MultivaluedMap<String, String> httpHeaders,
        InputStream entityStream) throws IOException, WebApplicationException {
      return marshaller.unmarshall(entityStream);
    }
  }
}
这意味着guice绑定工作正常!=)

Oct 23, 2012 5:17:11 PM com.sun.jersey.guice.spi.container.GuiceComponentProviderFactory getComponentProvider
INFO: Binding FooExample$FooReader to GuiceInstantiatedComponentProvider
Exception in thread "main" com.sun.jersey.api.client.UniformInterfaceException: PUT http://localhost:8080/foo/bar returned a response status of 404 Not Found
  at com.sun.jersey.api.client.WebResource.handle(WebResource.java:686)
  at com.sun.jersey.api.client.WebResource.access$200(WebResource.java:74)
  at com.sun.jersey.api.client.WebResource$Builder.put(WebResource.java:537)
  at FooExample.main(FooExample.java:28)