spring boot で swagger を設定する手順を教えてください
Spring BootプロジェクトにSwaggerを使うための手順は次のとおりです。
- プロジェクトのpom.xmlファイルにSwaggerの依存関係を追加する。例:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
- Swaggerの設定クラスを作成する:Swagger関連の属性や振る舞いを設定するためのSwaggerの設定クラスを作成する。このクラスは通常、@Configurationアノテーションでマークされ、@EnableSwagger2アノテーションでSwaggerを有効にする。たとえば:
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
}
- Swaggerプロパティ設定:必要に応じて、ドキュメントタイトル、ディスクリプション、バージョンなどをSwaggerの設定クラスで設定できます。例:
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build()
.apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("My API Documentation")
.description("API documentation for my Spring Boot project")
.version("1.0.0")
.build();
}
}
- Spring Bootアプリケーションを起動し、ブラウザでSwagger UIのURL、通常はhttp://localhost:8080/swagger-ui.html、にアクセスします。Swagger UIでは、APIのドキュメントとAPIをテストするためのインターフェースが表示されます。
Swagger を使用した Spring Boot プロジェクトの構成の基礎ステップはこれです。また、パッケージやパスによる API のフィルター、認証の設定など、必要に応じてさらに高度な構成を実行することもできます。