Simple Dropwizard 0.7.1 An Application Not Running the Optional QueryParam w / Java 8

I decided to return to Dropwizard after a very long business with Spring. I quickly got the barebone REST absolute REST service service, and it works without a problem.

Using Dropwizard 0.7.1 and Java 1.8 , only POM entries are dropwizard kernel dependencies and the maven compiler plugin to force Java 1.8, as recommended by the Dropwizard user guide.

However, as soon as I try to add an optional QueryParam to the base controller, the application cannot start with the following error (short 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@3 30103b7==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 the option from the mix, the application will work fine. I replace the optional code with null checks and it works fine.

Did I miss something fundamental here? Both Google Guava Optional and java.util.Optional crash with the same error. (And yes, I narrowed it down to an Option object)

A quick Google / SO search didn’t bring anything useful, but feel free to tell me a resource that I might have missed

Thanks in advance!

+6
source share
1 answer

Moments after posting this question, I found that the problem was using Java 1.8. If I use Java 1.8, I have to add Java8Bundle to my application:

POM Record:

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

And the code in the Application class:

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

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

This allows both the Google Guava Option and java.util.Optional to work fine.

If I go back to Java 1.7 and use Google Guava Optional, it works fine and I don’t need to enable Java8Bundle. I will choose Java8Bundle now, although using Java8 functions is beneficial for me :)

Hooray!

+11
source

Source: https://habr.com/ru/post/980475/


All Articles