2

I'm using Wildfly 10 and Java 8. I've tried to bind my service like here but I still getting this error. The only differences that I can see are on the pom.xml dependencies, but the 2.0-rc1 version is old.

[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

My API REST class

@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>

My Application JAX-RS class

public class ApplicationJaxRS extends ResourceConfig {

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

}

And lastly, my custom binder

public class CustomBinder extends AbstractBinder {

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

}

If you need more details, please tell me

Villat
  • 1,455
  • 1
  • 16
  • 33

2 Answers2

1

In the AbstractBinder, it should be bind(Implementation).to(Contract). You have it the other way around. The reason they do it this way is that it's possible for an implementation to have multiple contracts, so you would be able to just chain to calls.

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
1

Changing the CustomBinder implementation from :

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

to

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

should work for you.


On a very different note, I would suggest something along these lines, creating an interface:

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

and then implement it in class as

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

and bind them as

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

and further use it in your Resource class as:

ICuenta icuenta;

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


@GET
@Produces(MediaType.APPLICATION_JSON)
public Cuenta getCuenta() {
    return icuenta.getCeunta();
}
Naman
  • 27,789
  • 26
  • 218
  • 353