WebMvcConfigurer관련

함궈·2022년 7월 7일

스프링

목록 보기
1/5
@Configuration
@EnableWebMvc
public class MvcConfig implements WebMvcConfigurer {

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        registry.jsp("/WEB-INF/view/", ".jsp");
    }
}

BeanCreationException이 발생한다.

Error creating bean with name 'defaultServletHandlerMapping' defined in class path resource [org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration.class]

위 코드를 전부 주석처리하고,

@Slf4j
@Controller
public class HelloController {

    @GetMapping("/hello")
    public String hello(Model model, @RequestParam(value = "name", required = false) String name){

        model.addAttribute("greeting", "안녕하세요, " + name);
        log.info("왜안됨 {}", name);
        return "hello";
    }
}

위 경로로 요청했을 때, 로그가 출력되는지 보았다.

잘됨

WebMvcConfigurer 인터페이스를 구현한 HelloController클래스에 문제가 있음을 알 수 있었다.
오버라이딩한 메서드에 대해 찾아보니 web.xml에서 정의해둔 dispatcherServlet 매핑 경로에 대하여, 컨트롤러 애너테이션을 적용한 빈 객체를 찾지 못했을 때, SimpleUrlHandlerMapping을 사용해서 요청을 처리하도록 하는 목적으로 사용한다. 스프링 공식 문서

@Configuration
@EnableWebMvc
public class MvcConfig implements WebMvcConfigurer {

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable("/");
//        configurer.enable();
    }

    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        registry.jsp("/WEB-INF/view/", ".jsp");
    }
}

0개의 댓글