0

クライアントの観点から、GuiceインジェクションをJersey MessageBodyReader/MessageBodyWriterで機能させようとしています。サーバーはGuiceで正しく起動しています。私の問題はクライアントにあります。

エラーを示す次の項目をまとめました。SEVERE: Missing dependency for constructor public FooExample$FooReader(FooExample$FooUnmarshaller) at parameter index 0

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.google.inject.Injector;
import com.google.inject.Provides;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;

class FooExample {
  public static void main(String[] args) {
    Injector i = Guice.createInjector(new FooExample().new FooModule());
    WebResource service = i.getInstance(WebResource.class);

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

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

    @Provides
    public WebResource configuredClient() {
      ClientConfig config = new DefaultClientConfig();
      config.getClasses().add(FooReader.class);
      return Client.create(config).resource(
          UriBuilder.fromUri("http://localhost:8080/foo").build());
    }
  }

  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);
    }
  }
}

コンソール出力を取得する場所:

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

使用する必要があると感じていますがGuiceComponentProviderFactory、ドキュメントも.も見つからないようです。どんな助けでも大歓迎です。ありがとう!IoCComponentProviderFactoryClientConfig

4

1 に答える 1

1

そこで、自分の質問を推測して確認して解決しました。これが意図された方法であったことを確認することはできませんが、これは機能します。

Clientクラスにはメソッドがあります。それpublic static Client 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);
    }
  }
}

および出力

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)

guiceバインディングが機能したことを意味します!=)

于 2012-10-23T21:20:27.643 に答える