삽질 좋아/오류 해결
[Swagger] Springboot 3.x에 Swagger를 적용하기 (해결완)
싱브이
2024. 5. 9. 16:30
728x90
반응형
springfox가 Springboot 2.x까지만 잘 적용된다니..
swagger-ui가 열리지 않아서 고군분투 중 !
해결하자 언눙..
해결했다.
결론
Springboot3.x.x 버전에서는 springdoc-openapi-starter-webmvc-ui 을 사용할 것.
[build.gradle]
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.0.2'
* springfox와 관련된 gradle은 모두 삭제해도 됨 !!!!!
수정 전에는 예전 프로젝트여서 Springboot 2.x 버전이었다. 그래서 springfox를 사용해도 가능했지만, springfox가 열일하지 않아서 springboot 3 버전에서는 안된다 ㅠ
[gradle]
springboot 2.x.x → springfox
springboot 3.x.x → springdoc-openapi-starter-webmvc-ui
[Link]
springboot 2.x.x → localhost:8080/swagger-ui.html
springboot 3.x.x → localhost:8080/swagger-ui/index.html
코드 보기
[수정 전]
1. build.gradle
implementation 'io.springfox:springfox-boot-starter:3.0.0'
implementation 'io.springfox:springfox-swagger-ui:3.0.0'
2. SwaggerConfig
@Bean
public Docket api() {
return new Docket(DocumentationType.OAS_30)
.useDefaultResponseMessages(false)
.select()
.apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
.paths(PathSelectors.any())
.build()
.apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Boot Todo2 Project Swagger")
.build();
}
3. @ApiOperation
@ApiOperation("Sample GET doA")
@GetMapping("/doA")
public List<String> doA(){
return Arrays.asList("AAA", "BBB", "CCC");
}
[수정 후]
1. build.gradle
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.0.2'
2. SwaggerConfig (없어도 됨)
@Configuration
public class SwaggerConfig {
@Bean
public OpenAPI openAPI() {
return new OpenAPI()
.components(new Components())
.info(apiInfo());
}
private Info apiInfo() {
return new Info()
.title("Boot Todo2 Project Swagger")
.description("Swagger")
.version("1.0.0");
}
3. @Operation
@Operation(description="Sample GET doA")
@GetMapping("/doA")
public List<String> doA(){
return Arrays.asList("AAA", "BBB", "CCC");
}
728x90
반응형