2017-08-24 6 views
2

私はWildfly 10とJava 8を使用しています。サービスlike hereをバインドしようとしましたが、まだこのエラーが発生しています。私が見ることのできる唯一の違いはpom.xmlの依存関係ですが、2.0-rc1バージョンは古いものです。Jersey 2.22 UnsatisfiedDependencyException

[io.undertow.request] (default task-2) UT005023: Exception handling request to /api/account: javax.servlet.ServletException: A MultiException has 3 exceptions. They are: 
1. org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl(requiredType=ICuentaService,parent=CuentaServiceRS,qualifiers={},position=-1,optional=false,self=false,unqualified=null,287721117) 
2. java.lang.IllegalArgumentException: While attempting to resolve the dependencies of ar.com.olx.api.rest.CuentaServiceRS errors were found 
3. java.lang.IllegalStateException: Unable to perform operation: resolve on ar.com.olx.api.rest.CuentaServiceRS 

私のAPIのRESTクラス

@Path("/account") 
public class CuentaServiceRS { 

    @Inject 
    private ICuentaService cuentaService; 

    @GET 
    @Produces(MediaType.APPLICATION_JSON) 
    public Cuenta getCuenta() { 

     return cuentaService.getCuentas().get(0); 

    } 

} 

のpom.xml

<dependency> 
    <groupId>org.glassfish.jersey.media</groupId> 
    <artifactId>jersey-media-json-jackson</artifactId> 
    <version>2.22.2</version> 
</dependency> 

<dependency> 
    <groupId>org.glassfish.jersey.containers</groupId> 
    <artifactId>jersey-container-servlet</artifactId> 
    <version>2.22.2</version> 
</dependency> 

web.xmlの

<servlet> 
     <servlet-name>altitudeservlet</servlet-name> 
     <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> 
     <init-param> 
      <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name> 
      <param-value>true</param-value> 
     </init-param> 
     <init-param> 
      <param-name>javax.ws.rs.Application</param-name> 
      <param-value>ar.com.villat.bind.ApplicationJaxRS</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

マイアプリケーションJAX-RSクラス

あなたはより多くの詳細が必要な場合は
public class ApplicationJaxRS extends ResourceConfig { 

    public ApplicationJaxRS(){ 
     register(new CustomBinder()); 
     packages(true, "ar.com.villat"); 
    } 

} 

そして最後に、私のカスタムバインダー

public class CustomBinder extends AbstractBinder { 

    @Override 
    protected void configure() { 
     bind(ICuentaService.class).to(CuentaServiceImpl.class); 
    } 

} 

AbstractBinderで私に

答えて

1

を教えてください、それはbind(Implementation).to(Contract)する必要があります。あなたはそれを逆にしている。彼らがこのようにしている理由は、実装が複数の契約を持つ可能性があるため、toコールを連鎖させることができるからです。

+0

答えと説明をありがとうございます。 – Villat

1

からCustomBinderの実装を変更する:

@Override 
protected void configure() { 
    bind(ICuentaService.class).to(CuentaServiceImpl.class); 
} 

@Override 
protected void configure() { 
    bind(ICuentaService.class).to(CuentaServiceImpl.class); 
} 

にあなたのために働く必要があります。非常に異なるノートで


、私はインターフェイスを作成、これらの線に沿って何かを示唆している。その後、

public interface ICuenta { 
    // define an API 
    Cuenta getCeunta(); 
} 

public class ICuentaService implements ICuenta { 
    // implement the API 
    Cuenta getCeunta() { 
     // get it form somewher and return; 
    } 
} 

としてクラスでそれを実装し、それらを結合し

@Override 
protected void configure() { 
    bind(ICuenta.class).to(ICuentaService.class); 
} 

あなたのResourceクラスでさらに使用してください:

ICuenta icuenta; 

@Inject 
public CuentaServiceRS(ICuenta icuenta) { 
    this.icuenta = icuenta; 
} 


@GET 
@Produces(MediaType.APPLICATION_JSON) 
public Cuenta getCuenta() { 
    return icuenta.getCeunta(); 
} 
+1

ありがとう、答えの最初の部分が繰り返されますが、とにかくそれを理解しています。 – Villat

関連する問題