Spring Boot + Springfox springfox-boot-starter 3.0.0つかう


https://github.com/springfox/springfox をつかう。

build.gradle
plugins {
  id 'org.springframework.boot' version '2.3.2.RELEASE'
  id 'io.spring.dependency-management' version '1.0.9.RELEASE'
  id 'java'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
repositories {
  mavenCentral()
}
dependencies {
  implementation 'org.springframework.boot:spring-boot-starter-web'
  developmentOnly 'org.springframework.boot:spring-boot-devtools'
  testImplementation('org.springframework.boot:spring-boot-starter-test') {
    exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
  }
  // https://mvnrepository.com/artifact/io.springfox/springfox-boot-starter
  implementation 'io.springfox:springfox-boot-starter:3.0.0'
}
test {
  useJUnitPlatform()
}

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

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

    public static void main(String[] args) {
        SpringApplication.run(MainApp.class, args);
    }
}

これで起動して http://localhost:8080/swagger-ui/index.html にアクセスすると以下の画面が表示される。2.0以前とは異なりauto-configがあるのでswagger用のjava-configを作る必要がない。