zandero / rest.vertx

A JAX-RS like annotation processor for vert.x verticals and more
Apache License 2.0
160 stars 28 forks source link

Guice injection is not working #28

Closed tnguyen-lh closed 6 years ago

tnguyen-lh commented 6 years ago

I follow the instruction in "Injection" section, but got exception, the same exception happened in your example project.

java.lang.IllegalArgumentException: Failed to instantiate class of type: com.sc.application.rest.TestRest3, class needs empty constructor!

package com.sc.application.rest;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import com.google.inject.Guice;
import com.google.inject.Inject;

import io.vertx.core.json.JsonObject;

@Path("/test3")
public class TestRest3 {

    private  MyService service;

//  public TestRest3() {
//
//      service = Guice
//                .createInjector(new GuiceInjectionProvider())
//                .getInstance(MyService.class);
//  }

    @Inject
    public TestRest3(MyService someService) {

        service = someService;
    }

    @GET
    @Path("/test")
    @Produces(MediaType.APPLICATION_JSON)
    public JsonObject echo() {

        JsonObject message = new JsonObject();
        message.put("collection", "mycollection").put("document", new JsonObject().put("name", this.service.call()));

        return message;
    }
}

package com.sc.application.rest;

import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.zandero.rest.injection.InjectionProvider;

public class GuiceInjectionProvider extends AbstractModule implements InjectionProvider  {

    private Injector injector;

    public GuiceInjectionProvider() {
        injector = Guice.createInjector(this);
    }

    @Override
    protected void configure() {
        bind(MyService.class).to(MyServiceImpl.class);
    }

    @Override
    public Object getInstance(Class clazz) {
        return injector.getInstance(clazz);
    }
}

package com.sc.application.rest;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.handler.BodyHandler;

import java.util.HashMap;
import java.util.Map;

import com.sc.application.service.SomeDatabaseService;
import com.zandero.rest.RestBuilder;
import com.zandero.rest.RestRouter;

public class App extends AbstractVerticle {

    @Override
    public void start() {

        Router router = new RestBuilder(vertx)
                .injectWith(GuiceInjectionProvider.class)
                .register(TestRest3.class)
                .build();
        vertx.createHttpServer().requestHandler(router::accept).listen(8080);
    }

}

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.sc</groupId>
    <artifactId>sc-service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>sc-service</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <vertx.version>3.5.2</vertx.version>
    </properties>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/junit/junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>io.vertx</groupId>
            <artifactId>vertx-core</artifactId>
            <version>${vertx.version}</version>
        </dependency>

        <dependency>
            <groupId>io.vertx</groupId>
            <artifactId>vertx-sync</artifactId>
            <version>${vertx.version}</version>
        </dependency>

        <dependency>
            <groupId>io.vertx</groupId>
            <artifactId>vertx-service-proxy</artifactId>
            <version>${vertx.version}</version>
        </dependency>

        <dependency>
            <groupId>io.vertx</groupId>
            <artifactId>vertx-service-proxy</artifactId>
            <version>${vertx.version}</version>
            <classifier>processor</classifier>
        </dependency>

        <dependency>
            <groupId>io.vertx</groupId>
            <artifactId>vertx-service-proxy</artifactId>
            <version>${vertx.version}</version>
        </dependency>

        <dependency>
            <groupId>io.vertx</groupId>
            <artifactId>vertx-docgen</artifactId>
            <version>3.5.1</version>
        </dependency>

        <dependency>
            <groupId>io.vertx</groupId>
            <artifactId>vertx-codegen</artifactId>
            <version>${vertx.version}</version>
            <scope>provided</scope>
        </dependency>

<!--        <dependency> -->
<!--            <groupId>io.advantageous.qbit</groupId> -->
<!--            <artifactId>qbit-core</artifactId> -->
<!--            <version>2.0.0</version> -->
<!--        </dependency> -->

<!--        <dependency> -->
<!--            <groupId>io.advantageous.qbit</groupId> -->
<!--            <artifactId>qbit-vertx</artifactId> -->
<!--            <version>2.0.0</version> -->
<!--        </dependency> -->

<!--        <dependency> -->
<!--            <groupId>io.advantageous.qbit</groupId> -->
<!--            <artifactId>qbit-admin</artifactId> -->
<!--            <version>2.0.0</version> -->
<!--        </dependency> -->

<!--        <dependency> -->
<!--            <groupId>io.advantageous.qbit</groupId> -->
<!--            <artifactId>qbit-service-discovery</artifactId> -->
<!--            <version>2.0.0</version> -->
<!--        </dependency> -->

<!--        <dependency> -->
<!--            <groupId>io.advantageous.qbit</groupId> -->
<!--            <artifactId>qbit-guice</artifactId> -->
<!--            <version>1.1.0.RELEASE</version> -->
<!--        </dependency> -->

        <!-- https://mvnrepository.com/artifact/com.google.inject/guice -->
        <dependency>
            <groupId>com.google.inject</groupId>
            <artifactId>guice</artifactId>
            <version>4.2.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>25.1-jre</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
            <version>4.4.9</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.zandero/rest.vertx -->
        <dependency>
            <groupId>com.zandero</groupId>
            <artifactId>rest.vertx</artifactId>
            <version>0.8.4</version>
        </dependency>

    </dependencies>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.1</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

Starting App get exception

SEVERE: Failed in deploying verticle java.lang.IllegalArgumentException: Failed to instantiate class of type: com.sc.application.rest.TestRest3, class needs empty constructor! at com.zandero.rest.RestRouter.register(RestRouter.java:118) at com.zandero.rest.RestRouter.register(RestRouter.java:80) at com.zandero.rest.RestBuilder.getRouter(RestBuilder.java:328) at com.zandero.rest.RestBuilder.build(RestBuilder.java:360) at com.sc.application.rest.App.start(App.java:70) at io.vertx.core.AbstractVerticle.start(AbstractVerticle.java:106) at io.vertx.core.impl.DeploymentManager.lambda$doDeploy$8(DeploymentManager.java:483) at io.vertx.core.impl.ContextImpl.lambda$wrapTask$2(ContextImpl.java:339) at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:163) at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:404) at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:463) at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:886) at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) at java.lang.Thread.run(Thread.java:748)

drejc commented 6 years ago

The "problem" is you are using a Guice specific @Inject annotation instead of the standard javax.inject.Inject annotation.

Guice provides it's own set of JAX-RS annotations but rest.vertx uses javax.inject annotations.

Just change import com.google.inject.Inject;

to import javax.inject.Inject;

and it will work.

Will try to provide a more comprehensive error message to prevent additional confusion.