LoginSignup
3
4

More than 3 years have passed since last update.

Spring Boot で実装した REST API に swagger-ui を導入する

Posted at

概要

  • Spring Boot で REST API を実装している前提
  • API のドキュメントを生成する swagger-ui を導入する
  • gradle に io.springfox:springfox-boot-starter:3.0.0 を追加するだけ

サンプルプロジェクト

  • Spring Initializr で Spring Web を選択してプロジェクト新規作成
  • REST API を実装
    • src\main\java\com\example\demo\api\DemoController.java を以下内容で作成
package com.example.demo.api;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/api/demo")
public class DemoController {

    @GetMapping("/customers")
    public String[] customers() {
        return new String[] {
                "あいうえお",
                "かきくけこ",
                "さしすせそ"
        };
    }

    @GetMapping("/customer/{id}")
    public Map<String, String> customer(@PathVariable String id) {
        return Map.of(
                "id", id,
                "name", "名前",
                "address", "東京都");
    }
}

swagger-ui 導入

  • build.gradle に追加
build.gradle
    implementation "io.springfox:springfox-boot-starter:3.0.0"

参考

3
4
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
3
4