[Spring Boot] swagger 설정 방법
서버에서 제공해주는 API를 간단하게 테스트 해볼 수 있는 있는 도구인 Swagger 설정 방법에 대해 알아보겠다.
[수행 환경]
Windows 10 IntelliJ java 11 gradle-7.6 |
[Gradle 설정] |
//swagger implementation 'io.springfox:springfox-swagger2:2.9.2' implementation 'io.springfox:springfox-swagger-ui:2.9.2' |
SwaggerConfig 클래스 작성 |
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiKey; import springfox.documentation.service.AuthorizationScope; import springfox.documentation.service.SecurityReference; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spi.service.contexts.SecurityContext; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; import java.util.Arrays; import java.util.List; @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .useDefaultResponseMessages(false) .host("127.0.0.1:8081") .select() .apis(RequestHandlerSelectors.basePackage("com.test")) .paths(PathSelectors.ant("/v1/**")) .build() .securityContexts(Arrays.asList(securityContext())) .securitySchemes(Arrays.asList(apiKey())); } private ApiKey apiKey() { return new ApiKey("Authorization", "Authorization", "header"); } private SecurityContext securityContext() { return springfox .documentation .spi.service .contexts .SecurityContext .builder() .securityReferences(defaultAuth()).forPaths(PathSelectors.any()).build(); } List<SecurityReference> defaultAuth() { AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything"); AuthorizationScope[] authorizationScopes = new AuthorizationScope[1]; authorizationScopes[0] = authorizationScope; return Arrays.asList(new SecurityReference("Authorization", authorizationScopes)); } } |
[사용 예제] testController |
import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @Slf4j @RequestMapping("/v1") @RestController @AllArgsConstructor @Api(tags = {"01. 테스트 API"}) public class TestContorller { private final UserClient userClient; @GetMapping("/test") @ApiOperation(value = "test 테스트") public String test() { return "안녕"; } } |
[실행 후 접속 방법] |
http://127.0.0.1:8081/swagger-ui.html# |
반응형
'프로그램 > Spring Boot' 카테고리의 다른 글
[Spring Boot] JPA 설정 및 사용 방법 (0) | 2023.07.16 |
---|---|
[Spring Boot] Intellij를 이용한 Entity 자동 생성 (0) | 2023.07.15 |
[Spring Boot] 이미지 캐싱, CDN 기능 설정 (0) | 2023.07.10 |
[Spring Boot] QueryDsl 설정 방법 (0) | 2023.07.08 |
[Spring Boot] Feign Client 사용법 (0) | 2023.07.04 |