---
schema_version: "1"
id: b2ee2006d1fa0f80d21e77abdd0b8c749fca42a5bcc22cb516968682316a5722
title: Mule プロジェクトの APIkit プロジェクトへの変換
collection: mulesoft
version: current
locale: ja-jp
portal_url: https://docs.salesforce.com/mulesoft/current/ja-jp/apikit/latest/apikit-workflow-convert-existing.md
canonical_url: https://docs.mulesoft.com/jp/apikit/latest/apikit-workflow-convert-existing
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 1480264cfc89ccf6317d988d7db35cfca9aa64dd4e8d3ffb0110a74546676ce5
description: MuleSoft Documentation Site
release: 4.x
latest: true
---
# Mule プロジェクトの APIkit プロジェクトへの変換

ビジネスロジックを含む Mule プロジェクトとアプリケーションに対してバックエンドファーストの設計アプローチを選択した場合、それらを APIkit プロジェクトに変換し、REST サービスとして公開できます。次のいずれかのアプローチを使用します。

- [\[manual-approach\]](#manual-approach)
- [\[scaffolding-approach\]](#scaffolding-approach)

最適なアプローチを決定するときは、アプリケーションのサイズと複雑さを考慮してください。

これらのアプローチの例は、データベースからユーザーを取得する次の Mule アプリケーションに基づいています。

```xml
<mule>
    <http:listener-config name="HTTP_Listener_config">
		<http:listener-connection host="0.0.0.0" port="8081" />
	</http:listener-config>
	<db:config name="Database_Config">
		<db:my-sql-connection host="localhost" port="3306" user="admin" password="admin" database="usersdb" />
	</db:config>
	<flow name="GET-Users-FLOW">
		<http:listener config-ref="HTTP_Listener_config" path="/users"/>
		<db:select config-ref="Database_Config">
			<db:sql ><![CDATA[SELECT * FROM USERS]]></db:sql>
		</db:select>
	</flow>
	<flow name="GET-User-By-Id-FLOW">
		<http:listener config-ref="HTTP_Listener_config" path="/userById" />
		<db:select config-ref="Database_Config" >
			<db:sql ><![CDATA[SELECT * FROM USERS WHERE :userIdParameter == userID]]></db:sql>
			<db:input-parameters ><![CDATA[#[{userIdParameter: attributes.queryParams.id}]]]></db:input-parameters>
		</db:select>
	</flow>
</mule>
```

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

サービスの API 仕様を定義するには、​[「REST API の構築、実装、テスト」の「API 仕様の設計」](https://docs.mulesoft.com/jp/apikit/latest/apikit-4-implement-rest-api#design-your-api-specification)​を参照してください。この例を説明するために、そのワークフローで使用される API 仕様と JSON サンプルを ​`/src/main/resources/api`​ フォルダーに作成またはコピーします。

## [](#手動アプローチ)手動アプローチ

既存のフローで機能するルーターを手動で設定するには、次の手順に従います。

### [](#ステップ-1-メインフローを作成する)ステップ 1: メインフローを作成する

APIkit プロジェクト内で、エントリポイントとして機能するメインフローを作成します。

1. 次のコンポーネントを Mule パレットからキャンバスにドラッグします。
    
    - フローソースとしての ​**\[HTTP Listener (HTTP リスナー)\]**​ ソース
    - フロープロセス内の ​**\[APIkit Router (APIkit ルーター)\]**​ 操作
2. **\[HTTP Listener (HTTP リスナー)\]**​ のプロパティエディターの ​**\[Connector Configuration (コネクタ設定)\]**​ で、既存の ​**\[HTTP\_Listener\_Config\]**​ を選択します。
3. **\[Path (パス)\]** に ​`/api/*`​ を入力して、APIkit ルーターのリスナーパスを指定します。
4. APIkit ルーターのプロパティエディターの ​**\[Router Configuration (ルーター設定)\]**​ で、​**\[Add (追加)\]**​ をクリックして新しい設定を作成します。
    
    **\[Global Element Properties (グローバル要素のプロパティ)\]**​ ウィザードが表示されます。
5. **\[API Definition (API 定義)\]**​ で、​`api/api.raml`​ パスを選択します。
6. 設定を受け入れます。
7. `GET-Users-FLOW`​ から ​**\[HTTP Listener (HTTP リスナー)\]**​ を削除します。
8. `GET-User-By-Id-FLOW`​ から ​**\[HTTP Listener (HTTP リスナー)\]**​ を削除します。

```xml
<mule>
    <apikit:config outboundHeadersMapName="outboundHeadersMapName" httpStatusVarName="httpStatus" name="Router" api="api/api.raml" />
    <http:listener-config name="HTTP_Listener_config">
		<http:listener-connection host="0.0.0.0" port="8081" />
	</http:listener-config>
	<db:config name="Database_Config">
		<db:my-sql-connection host="localhost" port="3306" user="admin" password="admin" database="usersdb" />
	</db:config>
    <flow name="apiFlow">
		<http:listener config-ref="HTTP_Listener_config" path="/api/*"/>
		<apikit:router config-ref="Router"/>
	</flow>
	<flow name="GET-Users-FLOW">
		<db:select config-ref="Database_Config">
			<db:sql ><![CDATA[SELECT * FROM USERS]]></db:sql>
		</db:select>
	</flow>
	<flow name="GET-User-By-Id-FLOW">
		<db:select config-ref="Database_Config" >
			<db:sql ><![CDATA[SELECT * FROM USERS WHERE :userIdParameter == userID]]></db:sql>
			<db:input-parameters ><![CDATA[#[{userIdParameter: attributes.queryParams.id}]]]></db:input-parameters>
		</db:select>
	</flow>
</mule>
```

### [](#ステップ-2-api-リソースとアクションをフローにマップする)ステップ 2: API リソースとアクションをフローにマップする

デフォルトでは、各 APIkit フローはルーティング目的で、次のパターンを特徴とする命名規則に従います。

`action:\resource[:content-type][:config-name]`

<table class="tableblock frame-all grid-all stretch">
<colgroup>
<col style="width: 50%;">
<col style="width: 50%;">
</colgroup>
<thead>
<tr>
<th class="tableblock halign-left valign-top">名前</th>
<th class="tableblock halign-left valign-top">説明</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>action</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">HTTP メソッド</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>resource</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">HTTP リソース</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>content-type</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">ペイロードのコンテンツタイプ (省略可能)</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>config-name</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">APIkit ルーター設定名 (省略可能)</p></td>
</tr>
</tbody>
</table>

元のフロー名を保持し、各 API リソースとアクションをそれらにマップするには、次の手順に従ってこの動作を上書きします。

1. **\[APIkit Router (APIkit ルーター)\]**​ を選択して、​**プロパティエディター**​を開きます。
2. **\[Router configuration (ルーター設定)\]**​ で ​**\[Edit (編集)\]**​ をクリックします。
    
    **\[Global Element Properties (グローバル要素のプロパティ)\]**​ ウィザードが表示されます。
3. **\[Mappings (マッピング)\]**​ で、​`[Add (追加)]`​ をクリックして新しいマッピング設定を作成します。
    
    **\[New Mapping (新規マッピング)\]**​ ダイアログが表示されます。
4. ドロップダウンを使用して、リソースをアクションにマップします。
    
    - **\[Resource (リソース)\]**​ で ​`[/users]`​ を選択します。
    - **\[Action (アクション)\]**​ で ​**\[Get\]**​ を選択します。
    - **\[Flow (フロー)\]**​ で ​`[GET-Users-FLOW]`​ を選択します。
    - **\[OK\]**​ をクリックします。
5. **\[Mappings (マッピング)\]**​ で ​**\[Add (追加)\]**​ をクリックします。
    
    **\[New Mapping (新規マッピング)\]**​ ダイアログが表示されます。
6. ドロップダウンを使用して、リソースをアクションにマップします。
    
    - **\[Resource (リソース)\]**​ で ​`[/users/userbyid]`​ を選択します。
    - **\[Action (アクション)\]**​ で ​`[Get]`​ を選択します。
    - **\[Flow (フロー)\]**​ で、get users アクションを含むフロー ​`[GET-User-By-Id-FLOW]`​ を選択します。
    - **\[OK\]**​ をクリックします。

```xml
<mule>
    <apikit:config outboundHeadersMapName="outboundHeadersMapName" httpStatusVarName="httpStatus" name="Router" api="api/api.raml" >
		<apikit:flow-mappings >
			<apikit:flow-mapping resource="/users" action="get" flow-ref="GET-Users-FLOW" />
			<apikit:flow-mapping resource="/users/userbyid" action="get" flow-ref="GET-User-By-Id-FLOW" />
		</apikit:flow-mappings>
	</apikit:config>
    <http:listener-config name="HTTP_Listener_config">
		<http:listener-connection host="0.0.0.0" port="8081" />
	</http:listener-config>
	<db:config name="Database_Config">
		<db:my-sql-connection host="localhost" port="3306" user="admin" password="admin" database="usersdb" />
	</db:config>
    <flow name="apiFlow">
		<http:listener config-ref="HTTP_Listener_config" path="/api/*"/>
		<apikit:router config-ref="Router"/>
	</flow>
	<flow name="GET-Users-FLOW">
		<db:select config-ref="Database_Config">
			<db:sql ><![CDATA[SELECT * FROM USERS]]></db:sql>
		</db:select>
	</flow>
	<flow name="GET-User-By-Id-FLOW">
		<db:select config-ref="Database_Config" >
			<db:sql ><![CDATA[SELECT * FROM USERS WHERE :userIdParameter == userID]]></db:sql>
			<db:input-parameters ><![CDATA[#[{userIdParameter: attributes.queryParams.id}]]]></db:input-parameters>
		</db:select>
	</flow>
</mule>
```

## [](#スキャフォールディングアプローチ)スキャフォールディングアプローチ

[\[before-you-begin\]](#before-you-begin)のプロセスに従います。完了したら、API 仕様をスキャフォールディングするための代替ワークフローを含めます。

このアプローチでは、APIkit ルーター設定、エラーハンドラー、およびコンソールフローを標準で使用できます。各リソースをマップするフローも得ることができます。

### [](#ステップ-1-api-仕様をスキャフォールディングする)ステップ 1: API 仕様をスキャフォールディングする

API をスキャフォールディングして、メイン、コンソール、およびリソースフローを自動生成します。

1. **Package Explorer**​ で、​`/src/main/resources/api/api.raml`​ を右クリックします。
2. **\[Mule\]**​ を選択します。
3. **\[Generate Flows from Local REST API (ローカル REST API からフローを生成)\]**​ をクリックします。

API と同じ名前の新しい XML ファイルが作成されます。このファイルにはスキャフォールディングフローが含まれます。

### [](#ステップ-2-フローを参照する)ステップ 2: フローを参照する

次のいずれかの操作を実行します。

- [\[step-2-map-api-resources-and-actions-to-flows\]](#step-2-map-api-resources-and-actions-to-flows)で説明されているように、各リソースのスキャフォールディングフローを削除し、元のフローをマップする。
- スキャフォールディングフローからすべての操作を削除し、削除された操作を含むフロー参照を追加する。このオプションを選択すると、すべてのフローが操作を含むフローを参照するため、間接参照が追加されますが、シナリオによってはこのような間接参照は不便な可能性があります。

```xml
<mule>
...
    <flow name="get:\users\userbyid:api-config">
		<flow-ref name="GET-User-By-Id-FLOW"/>
    </flow>
    <flow name="get:\users:api-config">
		<flow-ref name="GET-Users-FLOW"/>
    </flow>
</mule>
```

- 既存のフローのコンテンツをルーティング命名規則に準拠したスキャフォールディングフローに転送する。アプリケーションが大きい場合、これらの変更を実行するのは手間がかかる可能性があります。

```xml
<mule>
...
    <flow name="get:\users:api-config">
		<db:select config-ref="Database_Config">
			<db:sql ><![CDATA[SELECT * FROM USERS]]></db:sql>
		</db:select>
	</flow>
	<flow name="get:\users\userbyid:api-config">
		<db:select config-ref="Database_Config" >
			<db:sql ><![CDATA[SELECT * FROM USERS WHERE :userIdParameter == userID]]></db:sql>
			<db:input-parameters ><![CDATA[#[{userIdParameter: attributes.queryParams.id}]]]></db:input-parameters>
		</db:select>
	</flow>
</mule>
```

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

新しいプロジェクトがセットアップされたので、API を​[テスト](https://docs.mulesoft.com/jp/apikit/latest/apikit-4-implement-rest-api#test-your-api-implementation)​する準備が整いました。

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

- [APIkit for REST Module リファレンス](https://docs.mulesoft.com/jp/apikit/latest/apikit-4-xml-reference)
- [APIkit REST エラー処理リファレンス](https://docs.mulesoft.com/jp/apikit/latest/apikit-error-handling-reference)