본문 바로가기
Spring

[Spring; 스프링] Spring MVC 자동구성 제어 / WebMvcConfigurer vs WebMvcConfigurationSupport / Swagger

by daddydontsleep 2024. 1. 5.
728x90
728x90

사진: Unsplash 의 Celine Sayuri Tagami

Spring Boot Version : 3.2.1

Java Version : 17

Swagger : 2.3.0

	// swagger
	implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.3.0'

새로운 프로젝트를 스프링부트 3.x 버전으로 해보고싶어서 간단하게 Controller, Service, Repository, Entity, Dto 등을 만들고 Security 설정을 해주고 Swagger 관련 설정들도 해줬다.

내 생각엔 모든게 완벽해 보였다.

하지만 스웨거 작동이 뭔가 삐걱거렸다.

내가 설정한 /v3/api-docs 가 나오는게 아니고 기본 페이지인 petstore 가 자꾸 기본으로 나오는것이다.

"https://petstore.swagger.io/v2/swagger.json"

이 URL로 된 페이지가 먼저 나오는 것이였다.

하루종일 삽질을 하다가 WebMvcConfig 파일을 유심히 들여다보다가 발견한것이다.

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {

	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("/assets/**").addResourceLocations("classpath:/assets/").setCachePeriod(60 * 60 * 24);
		registry.addResourceHandler("/swagger-ui/**").addResourceLocations("classpath:/META-INF/resources/webjars/swagger-ui/5.10.3/");
	}
}

WebMvcConfigurationSupport를 상속해서 구현을 하다보니 자동 스프링 MVC 구성이 안되는 것이였다.

자세한 설명은 좋은 블로그 들이 있으니 참고 바란다. (아래에 링크를 달아뒀다.)

블로그들 보고 MVC 구성을 자동으로 변경하였더니 잘 되었다.

바꾼 소스 코드는 아래와 같다.

import org.springframework.boot.autoconfigure.web.servlet.WebMvcRegistrations;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer, WebMvcRegistrations {

	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("/assets/**").addResourceLocations("classpath:/assets/").setCachePeriod(60 * 60 * 24);
	}
}

수동으로 resource를 등록했던 아래 코드를 지우고

registry.addResourceHandler("/swagger-ui/**").addResourceLocations("classpath:/META-INF/resources/webjars/swagger-ui/5.10.3/");

extends 대신에 implements로 바꾸어 주었다.

public class WebMvcConfig implements WebMvcConfigurer, WebMvcRegistrations

잘 모르겠다 하는 내용은 댓글을 남겨주세요.끝.

 

 

[reference]

https://ksyy.tistory.com/292

 

WebMvcConfigurationSupport과 WebMvcConfigurer의 차이로 알아본 스프링 MVC 자동구성 변경 방법

swagger 설정 파일 만들다가 configuration class에 extends WebMvcConfigurationSupport implements WebMvcConfigurer 함에 따라 결과가 달라져서 찾아보게 되었다. WebMvcConfigurationSupport https://docs.spring.io/spring-framework/docs/cu

ksyy.tistory.com

https://ywkim95.tistory.com/6

 

WebMvcConfigurer vs WebMvcConfigurationSupport

프로젝트 중 cors 이슈가 있어서 원인 파악을 하며 알게 되었습니다. http://honeymon.io/tech/2018/03/13/spring-boot-mvc-controller.html 블로그를 참고하여 작성하였습니다. 기존에 cors 이슈를 해결하기 위해 WebMv

ywkim95.tistory.com

https://honeymon.io/tech/2018/03/13/spring-boot-mvc-controller.html

 

[springboot] Spring MVC 자동구성 제어하기 - I'm honeymon(JiHeon Kim).

자동구성된 스프링 MVC 구성 조작하기 자동구성된 스프링 MVC 구성을 큰 변경없이 추가적인 조작하려면 다음과 같은 형태로 구성한다. 1 2 3 @Configuration public class WebMvcConfig implements WebMvcConfigurer, Web

honeymon.io

 

728x90
300x250