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

Spring-Boot の REST API の仕様書を Springfox 3 から Swagger で生成する (Maven)

Posted at

環境

  • Java 11
  • mvn

準備

ひながた

Spring Initializaer からWEBアプリを落としてくる

2021-05-06 20.52.33 start.spring.io a72a30157c05.png

APIの口を書く

DemoApplication に直接書いてもいいけどちゃんと独立したクラスにしましょう。

MyController.java
package com.example.demo;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@SpringBootApplication
public class MyController {
  @GetMapping("/hoge")
  public String hoge(){
    return "hoge";
  }
}

Springfox の追加

https://github.com/springfox/springfox
にあるように、以下の依存関係をMavenに追加する。「dependency」で探せばそれっぽいところがみつかるはず。

pom.xml
		<dependency>
				<groupId>io.springfox</groupId>
				<artifactId>springfox-boot-starter</artifactId>
				<version>3.0.0</version>
		</dependency>

起動してみる

なんとこれだけでうごくのである。 コマンドラインとか、Eclipse とか使ってる人は右クリックとか、とにかく mvn spring-boot:run を実行すると、(何かでポートが使われてなければ)8080番でサーバが起動する。

http://localhost:8080/hoge でAPIの応答が確認できる。

http://localhost:8080/swagger-ui/ に行けば以下のような画面が見れる筈。

2021-05-06 20.58.38 localhost 8673eb4d3455.png

ちゃんとクラス名まで拾ってくれる。尚API個別の説明とかは Springfox2 のアノテーションが使えるみたい。

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