220427 TIL (@EnableWebMvc)

Minseok-Choi·2022년 4월 27일
0

TIL

목록 보기
7/11

학습자료

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/servlet/config/annotation/EnableWebMvc.html
https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/servlet/config/annotation/WebMvcConfigurer.html
https://docs.spring.io/spring-framework/docs/current/reference/html/web.html#mvc-cors-controller
https://www.inflearn.com/questions/33417

학습계기

  • 프론트와 협업 중 CORS관련 에러 때문에 CORS를 해결하기 위해서 WebMvcConfigurer를 implements 해서 CORS를 맵핑해주었다.
  • 이전에 interceptor 관련해서도 구현했었기때문에 @Configuration만을 붙이고서 다시 배포 및 테스트를 요청했음에도 CORS 에러가 해결이 되지 않았다고 응답을 받았다.
  • 이후 다른 멤버에게 질문했을 때, @EnableWebMvc 어노테이션을 붙여보라는 의견을 받았고, 붙인 뒤 배포했을 때는 정상 작동한다는 응답을 받았다.
  • 어노테이션을 붙이지 않고도 정상 작동하는 코드도 있어서, 다시 테스트를 해볼 필요도 있을 것 같다.
  • 나와 비슷한 의문을 가진 질문에 대한 백기선님의 답변과 공식문서를 봐도, @EnableWebMvc 어노테이션 유무에 따른 차이를 정확하게 이해하지 못했다.
  • CORS 관련 공식예제에도 어노테이션이 부착되어있다.
@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry) {

        registry.addMapping("/api/**")
            .allowedOrigins("https://domain2.com")
            .allowedMethods("PUT", "DELETE")
            .allowedHeaders("header1", "header2", "header3")
            .exposedHeaders("header1", "header2")
            .allowCredentials(true).maxAge(3600);

        // Add more mappings...
    }
}
  • Spring 내부 로직에 대해서 아직 이해가 부족한 탓이 큰 것 같아서 내부구현에 대해서 정리가 우선된 후 다시 이 내용을 찬찬히 뜯어보고 다시 테스트도 해봐야겠다. 까먹지말고 다시 정리할 수 있길!
profile
차곡차곡

0개의 댓글