//swagger 기능 추가
implementation group: 'io.springfox', name: 'springfox-swagger2', version: '2.9.2'
//swagger ui 추가
implementation group: 'io.springfox', name: 'springfox-swagger-ui', version: '2.9.2'
2. allprojects에 repositories 추가
- 해당 내용을 추가하지 않으면 build가 되지 않는다.
2. SwaggerConfig 생성
폴더를 만들어서 config.swagger 밑에 SwaggerConfig를 생성했다.
다양한 SwaggerConfig를 생성할 수 있는 데, 모든 조건을 열어두는 방향으로 생성했다.
path등을 잘못 적어서 일부를 막아둔다면 큰 오류를 겪을 수 있다.
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
}
- Type은 Swagger2 버전이므로 SWAGGER_2로 고정시켜준다.
- apis와 paths는 any로 둔다.
3. 패키지 충돌로 인한 오류가 발생했을 때
에러 상황
Caused by: java.lang.NullPointerException: null
해당 에러가 발생하여 main에서 오류를 쫓아가면 밑 에러를 확인할 수 있다. Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 2022-01-31 20:16:49.779 ERROR 2245 --- [ main] o.s.boot.SpringApplication : Application run failed
org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException
이유는 여러 패키지로 인해 오류가 발생한 상황이다. 따라서 아래 application.yml을 추가해주면 된다.