/java/

Recommended init configuration for Spring Boot vs Dropwizard

2015-11-14 19:26:58

In this article I want to compare what we can find in "quick-start" pages on spring-boot#quick-start and dropwizard#getting-started

Spring Boot

For Spring boot we have to create 3 files. (Documentation say about 2 files but I want to split controller from init class)

pom.xml

<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>

<project http:="" maven-4.0.0.xsd&quot;="" maven.apache.org="" xmlns='"http://maven.apache.org/POM/4.0.0"' xmlns:xsi='"http://www.w3.org/2001/XMLSchema-instance"' xsd="" xsi:schemalocation='"http://maven.apache.org/POM/4.0.0'>
<modelversion>4.0.0</modelversion>
<groupid>pl.btbw</groupid>
<artifactid>spring-boot-test</artifactid>
<version>1.0-SNAPSHOT</version>
<parent>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-parent</artifactid>
<version>1.2.7.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-web</artifactid>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-maven-plugin</artifactid>
</plugin>
</plugins>
</build>
</project>

Application.java

package pl.btbw;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;

@ComponentScan
@EnableAutoConfiguration
public class Application {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }
}

SampleRestController

package pl.btbw.ctrl;

import org.springframework.web.bind.annotation.*;

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

@RestController
public class SampleRestController {

    @RequestMapping("/")
    @ResponseBody
    private Map

<string, object=""> home() {
        Map<string, object=""> response = new HashMap<string, object="">();
        response.put("title", "Lorem Ipsum");
        return response;
    }
}

this is all.

Now we can run your application from IDE by run a Application.main method or you have to execute maven package command and then execute this java -jar target/spring-boot-test-1.0-SNAPSHOT.jar

everything should by ok and you application should be ready to use :)

ok time for

Dropwizard

For Dropwizard we have to create 5 files. (It is possible to remove for example configuration.yml and HealthCheck, but I think it is recommended init stact)

pom.xml

<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>
<project http:="" maven-4.0.0.xsd&quot;="" maven.apache.org="" xmlns='"http://maven.apache.org/POM/4.0.0"' xmlns:xsi='"http://www.w3.org/2001/XMLSchema-instance"' xsd="" xsi:schemalocation='"http://maven.apache.org/POM/4.0.0'>
<modelversion>4.0.0</modelversion>
<groupid>pl.btbw</groupid>
<artifactid>dropwizard-test</artifactid>
<version>1.0-SNAPSHOT</version>
<properties>
<dropwizard.version>0.9.1</dropwizard.version>
</properties>
<dependencies>
<dependency>
<groupid>io.dropwizard</groupid>
<artifactid>dropwizard-core</artifactid>
<version>${dropwizard.version}</version>
</dependency>
</dependencies>
<build>
<pluginmanagement>
<plugins>
<plugin>
<groupid>org.apache.maven.plugins</groupid>
<artifactid>maven-jar-plugin</artifactid>
<version>2.6</version>
</plugin>
<plugin>
<groupid>org.apache.maven.plugins</groupid>
<artifactid>maven-shade-plugin</artifactid>
<version>2.4.1</version>
</plugin>
</plugins>
</pluginmanagement>
<plugins>
<plugin>
<groupid>org.apache.maven.plugins</groupid>
<artifactid>maven-jar-plugin</artifactid>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<adddefaultimplementationentries>true</adddefaultimplementationentries>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupid>org.apache.maven.plugins</groupid>
<artifactid>maven-shade-plugin</artifactid>
<version>2.3</version>
<configuration>
<createdependencyreducedpom>true</createdependencyreducedpom>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation='"org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/'>
<transformer implementation='"org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"'>
<mainclass>pl.btbw.HelloWorldApplication</mainclass>
</transformer>
</transformer></transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

configuration.yml

appName: DropwizardTest

YMLConfiguration.java

package pl.btbw;

import io.dropwizard.Configuration;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.hibernate.validator.constraints.NotEmpty;

public class YMLConfiguration extends Configuration {

    @NotEmpty
    private String appName;

    @JsonProperty
    public String getAppName() {
        return appName;
    }

    @JsonProperty
    public void setAppName(String appName) {
        this.appName = appName;
    }
}

AppNameHealthCheck.java

package pl.btbw.health;

import com.codahale.metrics.health.HealthCheck;

public class AppNameHealthCheck extends HealthCheck {

    private final String appName;

    public AppNameHealthCheck(String appName) {
        this.appName = appName;
    }

    @Override
    protected Result check() throws Exception {

        if (appName != null) {
            return Result.unhealthy("AppName doesn't exist");
        }
        return Result.healthy();
    }
}

HelloWorldResource.java

package pl.btbw.resources;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.HashMap;
import java.util.Map;

@Path("/")
@Produces(MediaType.APPLICATION_JSON)
public class HelloWorldResource {

    @GET
    public Map<string, object=""> sayHello() {
        Map<string, object=""> response = new HashMap<string, object="">();
        response.put("title", "Lorem Ipsum");
        return response;
    }
}

HelloWorldApplication.java

package pl.btbw;

import io.dropwizard.Application;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;
import pl.btbw.health.AppNameHealthCheck;
import pl.btbw.resources.HelloWorldResource;

public class HelloWorldApplication extends Application<ymlconfiguration> {

    public static void main(String[] args) throws Exception {
        new HelloWorldApplication().run(args);
    }

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

    @Override
    public void initialize(Bootstrap<ymlconfiguration> bootstrap) {

    }

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

        final HelloWorldResource resource = new HelloWorldResource();

        final AppNameHealthCheck healthCheck = new AppNameHealthCheck(configuration.getAppName());

        environment.healthChecks().register("template", healthCheck);
        environment.jersey().register(resource);
    }

}

Now we can run your application. First you have to execute maven package command and then execute this java -jar target/dropwizard-test-1.0-SNAPSHOT.jar server configuration.yml

everything should by ok and you application should be ready to use :)