ここでは、
Spring入門コンテンツ「Consuming Rest(RESTful Web サービスの使用)」をインポートする方法と、このコンテンツについて、紹介します。
以下の環境に入門コンテンツをインポートしました。
・Windows10 64bit
・Eclipse 2018-12(Eclipse 4.10)
・Java 8
・Spring Boot 2.1.3(Spring Tool Suite 4)
※下記の記事で構築した開発環境を利用
Spring Tool Suite 4の開発環境構築(Eclipseにインストール)|STS4
Spring入門コンテンツ「Consuming Rest(RESTful Web サービスの使用)」をインポートする方法と、このコンテンツについて、紹介します。
動作環境
以下の環境に入門コンテンツをインポートしました。
・Windows10 64bit
・Eclipse 2018-12(Eclipse 4.10)
・Java 8
・Spring Boot 2.1.3(Spring Tool Suite 4)
※下記の記事で構築した開発環境を利用
Spring Tool Suite 4の開発環境構築(Eclipseにインストール)|STS4
入門コンテンツのインポート
まず、Eclipseで「ファイル」>「新規」>「Spring 入門コンテンツのインポート」を選択します。
次に、「入門コンテンツのインポート」画面で「Consuming Rest」を選択して、「完了」ボタンをクリックして、インポートします。
・ビルドタイプ
Maven
・コードセット
完了をチェック(プロジェクト「gs-consuming-rest-complete」をインポート)
最後に、入門コンテンツ「Consuming Rest」がインポートされたことを確認します。
Consuming Rest(RESTful Web サービスの使用)
この入門コンテンツでは、SpringのRestTemplateを使用して、Web ページ・データを取得する方法を学習します。
RestTemplateについて
Springは、RestTemplateと呼ばれるテンプレートクラスを提供しています。
RestTemplateを使用して、RESTful WEB サービスを利用するアプリケーションを作成することができます。
RestTemplateは、RESTfulサービスとのやり取りを1行で済ませることができ、さらに、取得したデータをカスタムドメインタイプにバインドすることができます。
RESTfulサービス
この入門コンテンツでは、以下のURLで立ち上がっているRESTfulサービスを使用しています。SpringのRestTemplateを使用して、このRESTfulサービスからJSONドキュメントを取得するアプリケーションを作成しています。
・RESTfulサービス
WEBブラウザ、またはcurlで、このURLをリクエストすると、以下のようなJSONドキュメントが表示されます。
{
"type":"success",
"value":{
"id":8,
"quote":"I don't worry about my code scaling. Boot allows the developer to peel back the layers and customize when it's appropriate while keeping the conventions that just work."
}
}
Spring Boot アプリケーションの実行
「gs-consuming-rest-complete」を右クリックして、「実行」>「Spring Boot App」を選択します。
Spring Boot アプリケーションを実行すると、RESTfulサービスからJSONデータを取得して、Quoteオブジェクトにコンバートします。
このQuoteオブジェクトの内容をコンソールに出力していますので、以下の出力情報を確認して、実行結果を確認します。
・出力情報
2019-04-23 14:05:11.183 INFO 8252 --- [ main] hello.Application : Quote{type='success', value=Value{id=2, quote='With Boot you deploy everywhere you can find a JVM basically.'}}
アプリケーション(Application)
・アプリケーションクラス(RESTクライアント)
Application.java
・ソースコード
package hello;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
public class Application {
private static final Logger log = LoggerFactory.getLogger(Application.class);
public static void main(String args[]) {
SpringApplication.run(Application.class);
}
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder.build();
}
@Bean
public CommandLineRunner run(RestTemplate restTemplate) throws Exception {
return args -> {
Quote quote = restTemplate.getForObject(
"https://gturnquist-quoters.cfapps.io/api/random", Quote.class);
log.info(quote.toString());
};
}
}
mainメソッド
パラメータ
args - String配列(要素数0)
戻り値
なし
Spring Boot アプリケーションを実行すると、SpringApplication.runメソッドが実行されて、アプリケーションが起動します。
restTemplateメソッド
パラメータ
builder - RestTemplateBuilderオブジェクト
戻り値
RestTemplateオブジェクト
RestTemplateBuilderを使用して、RestTemplateを生成します。
生成したRestTemplateオブジェクトを返します。
* RestTemplateBuilderは、Springによってインジェクトされています。
runメソッド
パラメータ
restTemplate - RestTemplateオブジェクト
戻り値
CommandLineRunner
SpringApplication.runメソッドを実行すると、このApplication.runメソッドが実行されます。
RestTemplateを使用して、RESTfulサービスからJSONデータを取得して、Quoteオブジェクトにコンバートします。そして、Quoteオブジェクトの内容をコンソールに出力します。
* RestTemplate(HttpMessageConverterインターフェース)は、クラスパス上にあるJackson JSONライブラリを使用して、取得したJSONデータをQuoteオブジェクトにコンバートします。
* RestTemplate(HttpMessageConverterインターフェース)は、クラスパス上にあるJackson JSONライブラリを使用して、取得したJSONデータをQuoteオブジェクトにコンバートします。
* ここでは、RestTemplateを使用して、HTTP GETリクエストを作成していますが、POST、PUT、DELETEなど、他のリクエストもサポートしています。
* Applicationクラスは、容易にテストできるようなコーティングになっています。(簡単にモックできます)
ドメインクラス(Quoteクラス)
・Quoteクラス
Quote.java
・ソースコード
package hello;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)
public class Quote {
private String type;
private Value value;
public Quote() {
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Value getValue() {
return value;
}
public void setValue(Value value) {
this.value = value;
}
@Override
public String toString() {
return "Quote{" +
"type='" + type + '\'' +
", value=" + value +
'}';
}
}
Quoteクラスは、取得したJSONデータがセットされます。
@JsonIgnoreProperties(ignoreUnknown = true)
ドメインクラスにバインドされないJSONドキュメントのプロパティは無視することを示すために、Jackson JSONライブラリの @JsonIgnorePropertiesを付与しています。
ドメインクラスの変数名は、APIから返されたJSONドキュメント内のキーとまったく同じ変数名を指定して、JSONデータをドメインクラスに直接バインドしています。
ドメインクラスの変数名とJSONドキュメント内のキーが一致しない場合、@JsonPropertyを使用して、JSONドキュメント内のキーを指定する必要があります。
ドメインクラス(Valueクラス)
・Valueクラス
Value.java
・ソースコード
package hello;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)
public class Value {
private Long id;
private String quote;
public Value() {
}
public Long getId() {
return this.id;
}
public String getQuote() {
return this.quote;
}
public void setId(Long id) {
this.id = id;
}
public void setQuote(String quote) {
this.quote = quote;
}
@Override
public String toString() {
return "Value{" +
"id=" + id +
", quote='" + quote + '\'' +
'}';
}
}
Valueクラスは、取得したJSONデータのネストされたフィールドのデータがセットされます。
@JsonIgnoreProperties(ignoreUnknown = true)
上記、ドメインクラス(Quote)と同様。
参考
スポンサーリンク





0 件のコメント :
コメントを投稿