코딩/Error

[springboot error] Unable to render this definitionThe provided definition does not specify a valid version field.Please indicate a valid Swagger or OpenAPI version field. Supported version fields are swagger: "2.0" and those that match openapi: 3.0.n

eunslog 2024. 5. 7. 09:54

localhost:8080/swagger-ui/index.html로 들어갔는데 갑자기 이런 문구가 떴다..

난 처음에

build.gradle 또는 config 설정 문제라 생각했다.

 

저 에러 발생 후.. 며칠 간의 사투 끝에 해결했다..

내가 해결한 방법이다.

1. application.yml 수정
# Swagger
springdoc:
  default-consumes-media-type: application/json
  default-produces-media-type: application/json
  api-docs:
    groups:
      enabled: true
  swagger-ui:
    operations-sorter: alpha # alpha(알파벳 오름차순), method(HTTP메소드순)
    tags-sorter: alpha # 태그 정렬 기준
    path: /swagger # html 문서 접속 경로
    disable-swagger-default-url: true
    display-query-params-without-oauth2: true
    doc-expansion: none # tag, operation 펼치는 방식
  paths-to-match:
    - /**

 

2. build.gradle 확인
implementation group: 'org.springdoc', name: 'springdoc-openapi-starter-webmvc-ui', version: '2.1.0'
testImplementation group: 'org.springdoc', name: 'springdoc-openapi-starter-webmvc-api', version: '2.1.0'

 

난 원래 이렇게 작성했었다.

implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.0.4'

다른 블로그에서는 저렇게 해도 된 것 같더라... 뭔가 문제인지 잘 모르겠다

 

3. SwaggerConfig 수정

 

package org.pettopia.pettopiaback.config;

import io.swagger.v3.oas.annotations.OpenAPIDefinition;
import io.swagger.v3.oas.annotations.info.Info;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@OpenAPIDefinition(
        info = @Info(title = "Pettopia", version = "v1"))
@RequiredArgsConstructor
@Configuration
public class SwaggerConfig {

    @Bean
    public GroupedOpenApi chatOpenApi(){
        String[] paths = {"/**"};

        return GroupedOpenApi.builder()
                .group("Pettopia OPEN API v1")
                .pathsToMatch(paths)
                .build();
    }


}

이게 제일 중요한 것 같다.. 난 3번까지 하고나서 제대로 실행되었다.

 

도움 받은 블로그)

감사합니다..

https://velog.io/@phonil/Swagger-%EC%84%A4%EC%A0%95-feat.-spring-security

https://velog.io/@hwibinissuccess/Error%ED%95%B4%EA%B2%B0%EA%B8%B0%EB%A1%9D-Swagger-ui-%EA%B4%80%EB%A0%A8