6

I decided to return to Dropwizard after a very long affair with Spring. I quickly got the absolute barebones REST service built, and it runs without any problems.

Using Dropwizard 0.7.1 and Java 1.8, only POM entries are the dropwizard-core dependency and the maven compiler plugin to enforce Java 1.8, as recommended by the Dropwizard user manual

However, as soon as I try to add an Optional QueryParam to the basic controller, the application fails to start with the following error (cut for brevity):

INFO  [2015-01-03 17:44:58,059] io.dropwizard.jersey.DropwizardResourceConfig: The following paths were found for the configured resources:

    GET     / (edge.dw.sample.controllers.IndexController)

ERROR [2015-01-03 17:44:58,158] com.sun.jersey.spi.inject.Errors: The following errors and warnings have been detected with resource and/or provider classes:
SEVERE: Missing dependency for method public java.lang.String edge.dw.sample.controllers.IndexController.index(java.util.Optional) at parameter at index 0
Exception in thread "main" javax.servlet.ServletException: com.sun.jersey.spi.container.servlet.ServletContainer-6c2ed0cd@330103b7==com.sun.jersey.spi.container.servlet.ServletContainer,1,false

The code for the controller is as follows:

@Path("/")
public class IndexController {

    @GET
    @Timed
    public String index(@QueryParam("name") Optional<String> name) {
        String saying = "Hi";

        if(name != null && name.isPresent()) {
            saying += " " + name.get();
        }

        return saying;
    }
}

If I remove Optional from the mix, the application runs just fine. I replace the Optional-specific code with null checks and it works perfectly.

Am I missing something fundamental here? Both Google Guava Optional and java.util.Optional fail with the same error. (And yes, I did narrow it down to the Optional object)

A quick Google/SO search yielded nothing useful, but feel free to point me to a resource I may have missed

Thanks in advance!

1 Answer 1

11

Moments after posting this, I found that the issue was my use of Java 1.8. If using Java 1.8, I have to add the Java8Bundle to my app:

POM Entry:

<dependency>
    <groupId>io.dropwizard.modules</groupId>
    <artifactId>dropwizard-java8</artifactId>
    <version>0.7.0-1</version>
</dependency>

And code in the Application class:

@Override
public void initialize(Bootstrap<SampleConfiguration> bootstrap) {
    bootstrap.addBundle(new Java8Bundle());
}

See: https://github.com/dropwizard/dropwizard-java8

This enables both Google Guava Optional and java.util.Optional to work just fine.

If I revert to Java 1.7 and use the Google Guava Optional, it works just fine as well and I don't have to include the Java8Bundle. I'll opt for the Java8Bundle for now, though, as using Java8 features is lucrative for me :)

Cheers!

2
  • Guava Optional is by default handled by dropwizard, just FYI. Don't forget to mark the answer.
    – Natan
    Commented Jan 13, 2015 at 15:45
  • Thank you, that was a 45 min adventure of confused debugging.
    – Joe
    Commented Feb 4, 2016 at 6:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.