[java spring] Swagger 적용하기

Swagger 설정하기 위해서는 먼저 Spring Boot 프로젝트에 springfox-swagger2와 springfox-swagger-ui 라이브러리를 추가해야 합니다. 그리고 설정 클래스를 만들어 Swagger를 활성화해야 합니다.

  1. 라이브러리 추가:
<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>
  1. 설정 클래스 생성:
    “`java
    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.spi.DocumentationType;
    import springfox.documentation.spring.web.plugins.Docket;

@Configuration
public class SwaggerConfig {

@Bean
public Docket api() {
    return new Docket(DocumentationType.SWAGGER_2)
        .select()
        .apis(RequestHandlerSelectors.basePackage("your.package.controller"))
                .paths(PathSelectors.any())
                .build();
    }
}
```

3. Controller에 Swagger 어노테이션 추가:
Swagger 라이브러리로 생성된 API 문서를 노출시키기 위해 Controller에 Swagger 어노테이션을 추가해야 합니다.

```java
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

Leave a Comment