- build.gradle
dependencies {
// ...
implementation 'org.springdoc:springdoc-openapi-ui:1.6.9'
}
- application.yml ⁉️
springdoc:
swagger-ui:
path: /swagger-ui.html # Swagger HTML 문서 경로
groups-order: DESC
operationsSorter: alpha # 컨트롤러 내에서 정의한 api 메서드 순 (alpha:알파벳순/method:http method순)
disable-swagger-default-url: true
display-request-duration: true
api-docs:
path: /api-docs # spring boot 웹 애플리케이션의 api를 OpenAPI 3을 이용하여 json 형식화 한것의 경로
version: sample-project-backend.v0.1
show-actuator: true
default-consumes-media-type: application/json
default-produces-media-type: application/json
- config class
import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
/**
* swagger 설정파일
*/
@Configuration
public class OpenApiConfig {
@Bean
public OpenAPI openAPI(@Value("${springdoc.version}") String springdocVersion) {
Info info = new Info()
.title("Sample Project")
.version(springdocVersion)
.description("This is Sample Project Description.");
return new OpenAPI()
.components(new Components())
.info(info);
}
}
- controller class
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import api.common.dto.ResponseDto;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@Tag(name = "Tag-name", description = "this is the description.")
@RestController
public class SampleController {
@Operation(summary = "summary", description = "description")
@ApiResponses({
@ApiResponse(responseCode = "200", description = "요청 성공")
})
@PostMapping("/req")
public ResponseEntity<ResponseDto<Integer>> evalReq(
@Parameter(name = "파라미터1", description = "파라미터1", required = true) @RequestParam(value = "param1") Integer param1
) {
return ResponseEntity.ok().body(
ResponseDto.<String>builder()
.data("This is the result")
.message("success")
.build()
);
}
}
- 접속 화면