HubSpot/dropwizard-guice

0.8.0 Could not find a suitable constructor

Closed this issue · 2 comments

Hi,
I am new to dropwizard, thus also to dropwizard-guice. And i have problems getting the example to run. I tried rolling my own, because the example project linked in the readme is still using 0.7.x. I am just trying to get the dropwizard example working with guice.
When i run it and query my resource i get the 'Could not find a suitable constructor' exception.

My resource looks like this:

@Path("/hello-world")
@Produces(MediaType.APPLICATION_JSON)
@Resource
public class HelloWorldResource {
    private final String template;
    private final String defaultName;
    private final AtomicLong counter;

    public HelloWorldResource(@Named("template") String template, @Named("defaultName") String defaultName){
        this.template = template;
        this.defaultName = defaultName;
        this.counter = new AtomicLong();
    }

    @GET
    @Timed
    public Saying sayHello(@QueryParam("name") Optional<String> name) {
        final String value = String.format(template, name.or(defaultName));
        return new Saying(counter.incrementAndGet(), value);
    }
}

And my Application like this:

public class HelloWorldApplication extends Application<HelloWorldConfiguration> {
    public static void main(String[] args) throws Exception {
        new HelloWorldApplication().run(args);
    }

    @Override
    public String getName() {
        return "hello-world";
    }

    @Override
    public void initialize(Bootstrap<HelloWorldConfiguration> bootstrap) {
        GuiceBundle<HelloWorldConfiguration> guiceBundle = GuiceBundle.<HelloWorldConfiguration>newBuilder()
                  .addModule(new HelloWorldModule())
                  .enableAutoConfig(getClass().getPackage().getName())
                  .setConfigClass(HelloWorldConfiguration.class)
                  .build();

                bootstrap.addBundle(guiceBundle);
    }

    @Override
    public void run(HelloWorldConfiguration configuration,
            Environment environment) {
    }

Any help is appreciated (must be something i am overlooking).

I think you just need to add an @Inject annotation to your HelloWorldResource constructor.

Indeed that fixed it. Thanks for helping out.