---
schema_version: "1"
id: 1909e37d27b952c936b20d90fb00ea218980b820f0970abca30cd776977e4f6d
title: REST API の構築、実装、テスト
collection: mulesoft
version: current
locale: ja-jp
portal_url: https://docs.salesforce.com/mulesoft/current/ja-jp/apikit/latest/apikit-4-implement-rest-api.md
canonical_url: https://docs.mulesoft.com/jp/apikit/latest/apikit-4-implement-rest-api
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 9620181ea266534bca3ce19b36a2c187de4897f9915fcd77cee388eda617000b
description: MuleSoft Documentation Site
release: 4.x
latest: true
---
# REST API の構築、実装、テスト

このワークフローは、REST API の設計、実装、テストのプロセスを示しています。

例として、このワークフローではモックユーザー情報を JSON 形式で提供する無料のオンライン REST サービス [{JSON} Placeholder](https://jsonplaceholder.typicode.com)​ を使用します。

## [](#始める前に)始める前に

次のソフトウェアがインストールされていることを確認します。

- Anypoint Studio 7.1.4 以降
- Mule Runtime Engine 4.1.1 以降

## [](#api-仕様を設計する)API 仕様を設計する

Design Center の API Designer、Anypoint Studio の API 仕様プロジェクトツール、または任意のテキストエディターを使用して、API 仕様を設計します。

1. サービスが返す内容の例として使用するユーザーのリストを含む JSON ファイルを作成します。
    
    \[
      {
        "id": 1,
        "name": "Leanne Graham",
        "username": "Bret",
        "email": "Sincere@april.biz",
        "address": {
          "street": "Kulas Light",
          "suite": "Apt. 556",
          "city": "Gwenborough",
          "zipcode": "92998-3874",
          "geo": {
            "lat": "-37.3159",
            "lng": "81.1496"
          }
        },
        "phone": "1-770-736-8031 x56442",
        "website": "hildegard.org",
        "company": {
          "name": "Romaguera-Crona",
          "catchPhrase": "Multi-layered client-server neural-net",
          "bs": "harness real-time e-markets"
        }
      }
    \]
2. `user-example.json`​ として保存します。
3. 次の API 仕様を使用して新しい空のファイルを作成します。
    
    ```yaml
    #%RAML 1.0
    title: placeholder
    version: 1.0
    /users:
      get:
        description: Retrieve a list of all the users
        responses:
          200:
            body:
              application/json:
                example: !include user-example.json
      /userbyid:
        get:
          description: Get information about a particular user
          queryParameters:
            id:
              description: Specify the id of the user you want to retrieve
              type: integer
              required: false
              example: 3
          responses:
            200:
              body:
                application/json:
                  example: |
                    [
                      {
                        "id": 3,
                        "name": "Clementine Bauch",
                        "username": "Samantha",
                        "email": "Nathan@yesenia.net",
                        "address":
                          {
                            "street": "Douglas Extension",
                            "suite": "Suite 847",
                            "city": "McKenziehaven",
                            "zipcode": "59590-4157",
                            "geo": { "lat": "-68.6102", "lng": "-47.0653" },
                          },
                        "phone": "1-463-123-4447",
                        "website": "ramiro.info",
                        "company":
                          {
                            "name": "Romaguera-Jacobson",
                            "catchPhrase": "Face to face bifurcated interface",
                            "bs": "e-enable strategic applications",
                          }
                      }
                    ]
    ```
4. `api.raml`​ として保存します。
5. Studio で、​**\[File (ファイル)\]**​ > ​**\[New (新規)\]**​ > ​**\[Mule Project (Mule プロジェクト)\]**​ を選択します。
6. **\[Project Name (プロジェクト名)\]**​ で、Mule プロジェクトの名前を入力します。
    
    - Exchange から API 仕様をインポートする場合、​**\[Import a published API (パブリッシュ済みの API をインポート)\]**​ を選択し、プラスアイコンをクリックして ​**\[From Exchange (Exchange から)\]**​ を選択します。
        
        ![\*\[Import a published API (パブリッシュ済みの API をインポート)\]\* で、プラスをクリックして \[From Exchange (Exchange から)\] オプションを選択した状態が強調表示されています](_images/import-from-exchange.png)
    - ファイルシステムのローカルファイルから API 仕様をインポートする場合、​**\[Import RAML from Local File (ローカルファイルから RAML をインポート)\]**​ を選択します。
        
        ![\[API implementation (API 実装)\] セクションで \*\[Import RAML from Local File (ローカルファイルから RAML をインポート)\]\*が強調表示されています](_images/import-from-local-file.png)
    - Design Center から API 仕様をインポートする場合、​**\[Download RAML from Design Center (Design Center から RAML をダウンロード)\]**​ を選択します。
        
        ![\[API implementation (API 実装)\] セクションで \*\[Import from Design Center (Design Center からインポート)\]\* が強調表示されています](_images/import-from-design-center.png)
7. まだ選択されていない場合は、​**\[Scaffold Flows From These API Specifications (これらの API 仕様からフローをスキャフォールディングする)\]**​ を選択します。
8. **\[Finish (完了)\]**​ をクリックします。
    
    APIkit は、インポートした API 仕様に基づいて必要なフローを生成します。
9. **\[Run (実行)\]**​ > ​**\[Run As (別のユーザーとして実行)\]**​ > ​**\[Mule Application (Mule アプリケーション)\]**​ をクリックして APIkit プロジェクトを実行します。

## [](#api-コールをシミュレーションする)API コールをシミュレーションする

1. Anypoint Studio で、​**\[APIkit Consoles (APIkit コンソール)\]**​ ビューを見つけ、ベースポイント URL ​`http://localhost:8081/console/`​ の下にある ​**\[Open console (コンソールを開く)\]**​ をクリックします。
    
    ![ウィンドウの \[APIkit Consoles (APIkit コンソール)\] タブに基点 URL の場所が表示されています。](_images/apikit-consoles.png)
2. デフォルトのブラウザーが開きます。​**API Console**​ に、API の概要と、タイトル、バージョン、サポートされるプロトコルなどのプロジェクトに関する情報が表示されます。
    
    ![API Console バージョン情報](/mulesoft/current/ja-jp/apikit/latest/_images/apikit-console1.png)
3. **\[API Summary (API の概要)\]**​ で、​`/users`​ リソースの ​`[GET]`​ を選択します。
    
    リソースの詳細ページが開き、HTTP 200 応答の予想される JSON 例などが表示されます。
    
    ![APIkit 応答](/mulesoft/current/ja-jp/apikit/latest/_images/apikit-console3.png)
4. **\[API Summary (API の概要)\]**​ で、​`/userbyid`​ を展開します。
5. `/usersbyid`​ リソースの ​`[GET]`​ を選択します。
6. `[Try it (試す)]`​ をクリックして、要求を送信するためのビューを展開します。
    
    **API Console**​ に、要求を試すためのパラメーターとヘッダーを追加するセクションが表示されます。
7. 選択されていない場合は、​**\[Show optional parameters (省略可能なパラメーターを表示)\]**​ を選択します。
8. **id**​ パラメーターのクエリパラメーター値として​`「3」`​と入力し、​`[Send (送信)]`​ をクリックします。
    
    HTTP 200 応答は、​`/usersbyid`​ リソースに対する要求が成功したことを確認します。これには、ID 3 のユーザーのユーザー情報が含まれます。この応答には、JSON の例を含む本文も含まれます。
    
    ![id 3 のユーザーの情報が強調表示されています](/mulesoft/current/ja-jp/apikit/latest/_images/apikit-console4.png)

## [](#api-実装にビジネスロジックを追加する)API 実装にビジネスロジックを追加する

API からの例の代わりに、JSON Placeholder サービスの実際のデータを返すようにフローを変更します。

1. **\[Mule Palette (Mule パレット)\]**​ から新しい ​**\[Flow\]**​ コンポーネントをドラッグします。
2. 新しいフローの名前を​`「user_request_flow」`​に変更します。
3. **\[HTTP Request\]**​ コンポーネントをフロープロセスにドラッグします。
4. **\[General (一般)\]**​ の ​**\[Request (要求)\]**​ のプロパティエディターで、次の HTTP 要求オプションを設定します。
    
    - **\[Method (メソッド)\]**​ で、デフォルトの ​`[GET]`​ をそのままにします。
    - **\[Path (パス)\]**​ で、​`「/users」`​に設定します。
    - **\[Query Parameters (クエリパラメーター)\]**​ で、​**\[Switch to expression mode (式モードに切り替え)\]**​ を選択し、次の DataWeave スクリプトを貼り付けます。
        
        output application/java
        ---
        if (not isEmpty(attributes.queryParams))
        {
        	id : attributes.queryParams.id
        }
        else
        {}
5. `get:\users:api-config`​ フローと ​`get:\users\userbyid:api-config`​ フローの ​**\[Transform Message\]**​ コンポーネントを削除します。
6. 削除された ​**\[Transform Message\]**​ コンポーネントの代わりに、フローごとに 1 つの ​**\[Flow Reference\]**​ をドラッグします。
7. **\[Flow Reference\]**​ コンポーネントのプロパティエディターで、フロー名を​`「user_request_flow」`​として設定します。
8. 変更を保存します。

## [](#api-実装をテストする)API 実装をテストする

1. Package Explorer で、プロジェクト名を右クリックし、​**\[Run As (別のユーザーとして実行)\]**​ > ​**\[Mule Application (Mule アプリケーション)\]**​ を選択します。
2. ブラウザーを開き、​`http://localhost:8081/api/users`​ に移動します。
    
    JSON Placeholder サービスのユーザー情報が返されます。
    
    \[
       {
          "id": 1,
          "name": "Leanne Graham",
          "username": "Bret",
          "email": "Sincere@april.biz",
          "address": {
            "street": "Kulas Light",
            "suite": "Apt. 556",
            "city": "Gwenborough",
            "zipcode": "92998-3874",
            "geo": {
              "lat": "-37.3159",
              "lng": "81.1496"
    
        ...
        {
        "id": 10,
        "name": "Clementina DuBuque",
        "username": "Moriah.Stanton",
        "email": "Rey.Padberg@karina.biz",
        "address": {
          "street": "Kattie Turnpike",
          "suite": "Suite 198",
          "city": "Lebsackbury",
          "zipcode": "31428-2261",
          "geo": {
            "lat": "-38.2386",
            "lng": "57.2232"
          }
    ...
    \]
3. `http://localhost:8081/api/users/userbyid?id=4`​ に移動して、ID = 4 のユーザーに関する情報を取得します。
    
    \[
      {
        "id": 4,
        "name": "Patricia Lebsack",
        "username": "Karianne",
        "email": "Julianne.OConner@kory.org",
        "address": {
          "street": "Hoeger Mall",
          "suite": "Apt. 692",
          "city": "South Elvis",
          "zipcode": "53919-4257",
          "geo": {
            "lat": "29.4572",
            "lng": "-164.2990"
          }
        },
        "phone": "493-170-9623 x156",
        "website": "kale.biz",
        "company": {
          "name": "Robel-Corkery",
          "catchPhrase": "Multi-tiered zero tolerance productivity",
          "bs": "transition cutting-edge web services"
        }
      }
    \]

REST API の作成、実装、テストが完了しました。

## [](#関連情報)関連情報

- [{JSON} Placeholder](http://jsonplaceholder.typicode.com)
- [チュートリアル: API ビルドの最初から最後まで](https://docs.mulesoft.com/jp/general/api-led-overview)
- [Design Center を使用した API 仕様または API フラグメントの作成とパブリッシュ](https://docs.mulesoft.com/design-center/design-create-publish-api-specs)