스프링부트 swagger ui 추가하기 API 설명 웹 페이지

Posted by HULIA(휴리아)
2018. 4. 3. 22:55 백엔드개발/스프링부트

pom.xml

<dependency>

<groupId>io.springfox</groupId>

<artifactId>springfox-swagger2</artifactId>

<version>2.3.1</version>

</dependency>

<dependency>

<groupId>io.springfox</groupId>

<artifactId>springfox-swagger-ui</artifactId>

<version>2.3.1</version>

</dependency>



springbootapliction main 클래스에

@SpringBootApplication

@EnableSwagger2


추가해주면 자동으로 API관련된 정보들이 들어가게 됨




@Configuration

@EnableSwagger2

public class SwaggerConfig {

@Bean

public UiConfiguration uiConfig() {

return UiConfiguration.DEFAULT;

}

private ApiInfo metadata() {

return new ApiInfoBuilder()

.title("Spring Boot")

.description("Spring boot REST API")

.version("1.0")

.build();

}

@Bean

public Docket api() {

return new Docket(DocumentationType.SWAGGER_2)

.select()

.apis(RequestHandlerSelectors.any())

//.paths(PathSelectors.any()) // 모든 클래스 다 나옴

.paths(PathSelectors.ant("/test/**")) // 정해진 클래스만 나오도록 함

.build()

.apiInfo(metadata());

}

}

이렇게 Config클래스를 만들어서 설정해주면 세팅이 됨~



추가적으로 swagger 페이지에 API문서를 더 자세히 적고 싶으면 어노테이션을 추가로 하면 작성하면 된답니다

https://github.com/swagger-api/swagger-core/wiki/Annotations



사용예시사이트

https://steemit.com/kr-dev/@igna84/spring-boot-web-swagger

https://jojoldu.tistory.com/31