Dropwizardでindex.htmlをデフォルトページにする(Multiple servlets map to path: /*:を回避する)
今回の目的
Dropwizardを使って
1. index.html をデフォルトページに設定する
2. Multiple servlets map to path: /*:のエラーが出てこないように回避する
実行環境
- java バージョン: 8
- dropwizard バージョン: 0.9.2
今回のディレクトリ構成
TestProject
├── src/main/java
└── priv.jp.y.suzuki
├── TestApplication.java
└── TestConfiguration.java
├── src/main/resources
└── assets
└── index.html
├── pom.xml
└── testCongig.yml
1. index.html をデフォルトページに設定する
手順
- Applicationクラスに記載を行う
- pom.xml に記載を行う
1. Applicationクラスに記載を行う
Applicationクラスを継承したクラスのinitializeメソッドにおいて、addBundle(new AssetsBundle(String resourcePath, String uriPath, String indexFile))で指定する
TestApplication.java
TestApplication.java
package priv.jp.y.suzuki;
import io.dropwizard.Application;
import io.dropwizard.assets.AssetsBundle;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;
public class TestApplication extends Application<TestConfiguration>{
public static void main(String[] args) throws Exception {
new TestApplication().run(args);
}
@Override
public void initialize(Bootstrap<TestConfiguration> bootstrap) {
// assets配下に置いたindex.htmlをデフォルトページにする
bootstrap.addBundle(new AssetsBundle("/assets", "/", "index.html"));
}
@Override
public void run(TestConfiguration conf, Environment env) throws Exception {
}
}
2. pom.xml に記載を行う
pom.xmlに必要なライブラリを記載する
pom.xml
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>priv.jp.y.suzuki</groupId>
<artifactId>TestProject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<dropwizard.version>0.9.2</dropwizard.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-core</artifactId>
<version>${dropwizard.version}</version>
</dependency>
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-auth</artifactId>
<version>${dropwizard.version}</version>
</dependency>
<dependency> <!--TestApplicationクラスでimport io.dropwizard.assets.AssetsBundle;に必要 -->
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-assets</artifactId>
<version>${dropwizard.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<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>priv.jp.y.suzuki.TestApplication</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
2. Multiple servlets map to path: /*:を回避する
上記1.の手順のみで起動しようとすると「Multiple servlets map to path: /*:」のようなエラーが出てしまう(パスが競合するため)。
そのため、下記のようにymlにrootPathを記載することで回避できる
testConfig.yml
testConfig.yml
# Application server setting.
server:
applicationConnectors:
- type: http
port: 8090
rootPath: '/api/*' # ここを記載.