LoginSignup
5
1

More than 5 years have passed since last update.

[Java][Spring Boot][JAX-RS] Spring BootでJAX-RSを使う - NetBeansで始めるSpring Boot (2)

Posted at

Hello World!を作成しました

Spring Bootは、Spring MVCではなくJAX-RS実装であるところのJerseyも使えるそうなので、慣れているこちらを使ってみることにします。

pom.xmlを変更

依存性にあるspring-boot-starter-webspring-boot-starter-jerseyへ書き換えます。

Hello Controllerを削除

前回作成したHello Controllerは、Spring MVCのファイルだったみたいです。
よーしらんけど。

使わないので消してしまいます。

Jerseryを使う

リソースファイルの作成

@Componentアノテーションが必要みたいです。

HelloResource.java
package com.example.resource;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import org.springframework.stereotype.Component;

@Component
@Path("/")
public class HelloResource {

    @GET
    public String index() {
        return "Hello Spring Jersey!";
    }
}

アプリケーションファイルの作成

JAX-RSリソースの場所を教えるためのクラスを作成します。

JerseyConfig.java
package com.example;

import javax.ws.rs.ApplicationPath;
import org.glassfish.jersey.server.ResourceConfig;
import org.springframework.stereotype.Component;

@Component
@ApplicationPath("/rs")
public class JerseyConfig extends ResourceConfig {

    public JerseyConfig() {
        packages("com.example.resource");
    }

}

@ApplicationPathアノテーションで、パスを設定しています。

ビルドして実行

ビルドして実行したら、 http://localhost:8080/rs/ へアクセスしてみます。

Screenshot from 2016-10-28 18-02-48.png

こんなん表示されました。

5
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
1