Dropwizard Flash

I created a sample Dropwizard application for displaying flash messages between requests using the new Dropwizard 7.1 flash functionality. Uses Mustache for views and Gradle for builds.

Created this as an example as I saw the flash/session function was added in v7 but the Dropwizard documentation doesn’t give any details about how to use it, just a small line in the release notes.

Turns out it’s quite easy, just:

  • Add the Dropwizard FlashProvider and the Jersey SessionHandler in your Application run method
    public class ExampleApplication extends Application<ExampleConfiguration> {
        public void run(ExampleConfiguration configuration, Environment environment) {
            final UserResource userResource = new UserResource();
            environment.servlets().setSessionHandler(new SessionHandler());
  • Inject the flash object into your resource methods to get/set
    public class UserResource {
        public UsersView getAll(@Session Flash<String> message){
            String flashMessage = message.get().or("");
        public  UsersView flash(@Session Flash<String> message) throws URISyntaxException {
            message.set("Flash aaahhhh! He saved everyone of us!");

If you get the error below it’s because you forgot to add the FlashProvider/SessionHandler:

com.sun.jersey.spi.container.ContainerRequest: A message body reader for Java class io.dropwizard.jersey.sessions.Flash, and Java type io.dropwizard.jersey.sessions.Flash<java.lang.String>, and MIME media type application/octet-stream was not found.

Still have an issue with testing the resource, as I can’t find a way to inject or mock the Jetty SessionHandler needed to inject the Flash into the resource methods while still using the views. Dropwizard wizard samples use either LowLevelAppDescriptor or ResourceTestRule to do either, but never both.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s