|
| 1 | +## 관련 링크 |
| 2 | + |
| 3 | +[swagger yaml import 하는 방법 링크](https://sunghs.tistory.com/149) |
| 4 | +[Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException에러 발생 시](https://goyunji.tistory.com/137) |
| 5 | + |
| 6 | +```java |
| 7 | +import org.springframework.context.annotation.Configuration; |
| 8 | +import org.springframework.context.annotation.Primary; |
| 9 | +import org.springframework.core.io.Resource; |
| 10 | +import org.springframework.core.io.support.PathMatchingResourcePatternResolver; |
| 11 | +import springfox.documentation.spi.DocumentationType; |
| 12 | +import springfox.documentation.swagger.web.SwaggerResource; |
| 13 | +import springfox.documentation.swagger.web.SwaggerResourcesProvider; |
| 14 | + |
| 15 | +import java.io.IOException; |
| 16 | +import java.util.Arrays; |
| 17 | +import java.util.Collections; |
| 18 | +import java.util.List; |
| 19 | +import java.util.stream.Collectors; |
| 20 | + |
| 21 | +@Primary |
| 22 | +@Configuration |
| 23 | +public class SwaggerSpecConfig implements SwaggerResourcesProvider { |
| 24 | + |
| 25 | + @Override |
| 26 | + public List<SwaggerResource> get() { |
| 27 | + PathMatchingResourcePatternResolver patternResolver = new PathMatchingResourcePatternResolver(); |
| 28 | + try { |
| 29 | + Resource[] resources = patternResolver.getResources("/static/swagger/swagger.yaml"); |
| 30 | + if (resources.length == 0) { |
| 31 | + return Collections.emptyList(); |
| 32 | + } |
| 33 | + return Arrays.stream(resources).map(resource -> { |
| 34 | + SwaggerResource swaggerResource = new SwaggerResource(); |
| 35 | + swaggerResource.setSwaggerVersion(DocumentationType.SWAGGER_2.getVersion()); |
| 36 | + swaggerResource.setName(resource.getFilename()); |
| 37 | + swaggerResource.setLocation("/swagger/"+resource.getFilename()); |
| 38 | + return swaggerResource; |
| 39 | + }).collect(Collectors.toList()); |
| 40 | + } catch (IOException e) { |
| 41 | + e.printStackTrace(); |
| 42 | + return Collections.emptyList(); |
| 43 | + } |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | + |
| 48 | +``` |
| 49 | + |
| 50 | +spring: |
| 51 | +mvc: |
| 52 | +pathmatch: |
| 53 | +matching-strategy: ant_path_matcher |
| 54 | + |
| 55 | +``` |
| 56 | +위 코드를 application.yml에 적용시켜줍니다. |
| 57 | +``` |
0 commit comments