728x90

Java 17, Spring Boot 3.0.x 이상은 도대체 무슨 격동이 일어난 것일까..
기존에 사용하던 세팅이 지원이 안 되는 경우가 많다.

Swagger 역시 springfox로 설정하여 접속하게 되면 403 에러가 발생하면서 접속 자체가 막혀버리는 현상이 일어났다.
스택오버플로우를 찾아보니 springfox-boot-start 3.0.0, springdoc-openapi-ui 1.6.13 등이 아직 spring boot 3.0을 지원하지 않고 있는 것 같다.

우리가 사용할 수 있는 방법은 springdoc-openapi-starter-webmvc-ui 사용하는 방법이었다

의존성 추가

implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.0.2'
다음 의존성을 gradle에 추가하였다.

SwaggerConfig

@OpenAPIDefinition(
        info = @Info(
                title = "Content I Like API Docs",
                description = "API 명세서",
                version = "v1"
        )
)
@Configuration
public class SwaggerConfig {

}

application.yml 설정

springdoc:
  default-consumes-media-type: application/json
  default-produces-media-type: application/json
  api-docs:
    groups:
      enabled: true

  swagger-ui:
    path: /swagger-ui # 접속 path 설정
    disable-swagger-default-url: true
    doc-expansion: none # tag, operation 펼치는 방식
  paths-to-match:
    - /**

접속

저는 로컬 환경에서 확인 할 예정이므로
http://localhost:8088/swagger-ui/index.html 다음 url을 통하여 swagger에 접속했는데

다음과 같이 403 에러가 발생하면서, 접속이 되지 않았다.

해당 문제는 Spring Security에서 경로를 열어주면 된다.

 

Spring Security 설정

@Configuration
@EnableWebSecurity
@RequiredArgsConstructor
public class SecurityConfig {

    ...
    private final String[] SWAGGER = {"/v3/api-docs/**", "/swagger-ui/**"};

    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http
                .csrf()
                .disable()
                .authorizeHttpRequests()
                ...
                .requestMatchers(SWAGGER)
                .permitAll()
                ...
        return http.build();
    }
}

결과

 

swagger의 화면이 로드되지 않는 에러 발생시 참고

 

[Swagger Error] Spring boot 3.0.x 이상 springdoc-openapi Swagger 화면이 로드되지 않을 때.

다음과 같이 swagger-ui에 접속은 되는데, 내용들이 불러와지지 않고 하얀 화면만 보이는 증상이 발생했다. Spring Security 경로도 열어줬고, 설정도 다 했는데 왜 안될까 고민하며 F5만 누르다가 혹시

chordplaylist.tistory.com

참조

 

Springdoc-openapi(swagger) 설정

spring 프로젝트에 swagger 적용하기.

velog.io

 

 

How to run Swagger 3 on Spring Boot 3

Using a fresh Spring Initialzr with Java17 and Spring Boot 3.0.0, and an extra addition to the pom.xml for Springfox Swagger 3, I can't for the life of me get Swagger pages to work. Instead, I get ...

stackoverflow.com

 

springdoc-openapi v2.0.2

springdoc-openapi java library helps to automate the generation of API documentation using spring boot projects. springdoc-openapi works by examining an application at runtime to infer API semantics based on spring configurations, class structure and vario

springdoc.org

 

반응형
코드플리