IK.AM

@making's tech note


SpringBoot 1.2でJersery (JAX-RS)を使う

🗃 {Programming/Java/org/springframework/boot}
🏷 JAX-RS 🏷 Java 🏷 Jersey 🏷 Spring 🏷 Spring Boot 
🗓 Updated at 2014-10-11T11:50:52Z  🗓 Created at 2014-10-11T11:50:52Z   🌎 English Page

Spring Boot 1.2.M2からJerseryサポートが含まれたので使ってみました。

基本的には、pom.xmlに以下のようにspring-boot-starter-jerseyを依存関係に追加するだけ。(当然GradleでもOK) いままでSpring MVCで開発するときに指定していたspring-boot-starter-webの代替にすればOKです。

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.2.0.M2</version>
    <relativePath/>
    <!-- lookup parent from repository -->
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jersey</artifactId>
    </dependency>
</properties>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

<!-- まだ正式リリースじゃないからリポジトリ指定が必要 -->
<repositories>
    <repository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>http://repo.spring.io/milestone</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>
<pluginRepositories>
    <pluginRepository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>http://repo.spring.io/milestone</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </pluginRepository>
</pluginRepositories>

エントリポイント

package com.example;

import org.glassfish.jersey.server.ResourceConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;

@EnableAutoConfiguration
@ComponentScan
public class App {

    @Component
    static class JerseryConfig extends ResourceConfig {
        public JerseryConfig() {
            packages(true, "com.example");
        }
    }

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

Jerseyの設定が必要っぽいです。

リソースクラス

package com.example;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.ws.rs.GET;
import javax.ws.rs.Path;

@Component
@Path("hello")
public class Hello {
    @Value("${hello.name:World}")
    String name;

    @GET
    public String hello() {
        return "Hello " + name + "!";
    }

}

@ComponentでSpringの管理対象にすると諸々DIできるようになりました。ここでは@Valueを使う例を挙げました。

あとはAppを実行するだけ。mvn packageで実行可能jarもできます。

「Spring Bootに興味があっても、Sprign MVCはちょっと・・・」って方に是非!って感じですね。 JAX-RSを使ってもSpring Bootのメリットを享受できます。 (Dropwizardはもはやオワコンなのでは・・・)

APサーバーをデフォルトのTomcatからJettyに変え、組み込みサーバーを使ったREST APIのテストサンプル付きの検証アプリをGithubに置いておきました。


✒️️ Edit  ⏰ History  🗑 Delete