-
Swagger UI 적용하기Computer Science/Swagger 2022. 7. 6. 15:15
implementation group: 'io.springfox', name: 'springfox-swagger2', version: '2.9.2' implementation group: 'io.springfox', name: 'springfox-swagger-ui', version: '2.9.2'
2가지 gradle을 추가해준다.
package com.lofserver.soma.config; import com.google.common.base.Predicate; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.service.ResponseMessage; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; import java.time.ZonedDateTime; import java.util.List; @Configuration @EnableSwagger2 @EnableAutoConfiguration public class SwaggerConfig { private final String version = "v1"; private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("User API") .description("User 관련 API들 입니다.") .build(); } @Bean public Docket commonApi() { return new Docket(DocumentationType.SWAGGER_2) .groupName(version) .apiInfo(this.apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.lofserver.soma")) .paths(PathSelectors.any()) .build(); } @Bean public Docket postsApi() { return new Docket(DocumentationType.SWAGGER_2)//.groupName("public-api") .groupName("") .directModelSubstitute(LocalDateTime.class, String.class) .directModelSubstitute(LocalDate.class, String.class) .directModelSubstitute(LocalTime.class, String.class) .directModelSubstitute(ZonedDateTime.class, String.class) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class)) .paths(PathSelectors.any()) .build(); } }
config 파일을 만들어준다.
첫번째 함수에는 title과 설명이 들어간다.
두번째 함수는 version과 그 version에 들어갈 패키지를 설정해준다.
세번째 함수는 swagger 에서 LocalTime 클래스에 대해 string으로 출력이 되지 않고 class로써 출력이 되어 알아본 결과이다.
해당 클래스를 어떠한 방식으로 출력할지를 결정해주었다. ex) LocalDateTime class -> String class
Caused by: java.lang.NullPointerException: Cannot invoke "org.springframework.web.servlet.mvc.condition.PatternsRequestCondition.toString()" because the return value of "springfox.documentation.spi.service.contexts.Orderings.patternsCondition(springfox.documentation.RequestHandler)" is null
해당 오류가 생긴다면 @EnableSwagger2를 제거해준다.'Computer Science > Swagger' 카테고리의 다른 글
Swagger Annotation (0) 2022.07.06