0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【OpenAPI】swagger × prismでモックサーバーを構築

Posted at

概要

前回swaggerでAPIドキュメントを作成したので今回は作成したドキュメントをモックサーバーとして活用する方法をまとめました。
前回の記事は以下のリンクから遷移できます。

docker-compose.ymlについて

前回との差分の解説を書いていきます。

docker-compose.yml
prism:
  image: stoplight/prism:3
  volumes:
    - ./swagger.yml:/swagger.yml
  # prismは4010で立ち上がるのでとりあえず3000を指定
  ports:
    - "3000:4010"
  # -h でホストを指定してからモックサーバーとして使用するファイルを指定
  command: mock -h 0.0.0.0 /swagger.yml
  # swagger.ymlが作成されてから起動
  depends_on:
    - "swagger-merger"
swagger.yml
/user:
 get:
  ~ 省略 ~
  responses:
    '200':
       description: ユーザーデータ
       content:
         application/json:
           schema:
             properties:
               id:
                 example: 61798c29-d0e3-472f-91dd-db912e602249
                 type: string
               name:
                 example: ユーザー名
                 type: string
             type: object

ターミナルで以下を実行

$ curl http://localhost:3000/user

exampleで指定した内容が返ってきます

{"id":"61798c29-d0e3-472f-91dd-db912e602249","name":"ユーザー名"}

まとめ

開発初期にAPIのドキュメント作成とモックサーバーの構築を行うことでフロントエンドとバックエンドを並行して開発を進められるのでかなり便利だと感じています。

0
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?