컨트롤러 없이 요청 받는 법

장서연·2022년 3월 27일
0

간단히 뷰만 보여주는 경우, 굳이 컨트롤러를 만들지 않고 뷰를 보여줄 수 있는 방법이 있다.

원래대로라면,

@RestController
public class HomeController{
	@GetMapping("/hello")
    public String hello(){
    	return "hello";
    }
}

이렇게 컨트롤러를 직접 만들어줬었지만, 이렇게 달랑 뷰만 리턴하는 경우 굳이 컨트롤러를 이렇게 만들지 않아도 되는 방법이 있다는 말이다.

바로, WebMvcConfigurer 를 implement 한 WebConfig 클래스를 생성하여 addViewConrollers 함수를 오버라이딩 해주는 것!

@Configuration
public class WebConfig implements WebMvcConfigurer {
	@Override
    public void addViewConrollers(ViewControllerRegistry registry){
    	registry.addViewConroller("/hello").setViewName("hello");
    }
}

0개의 댓글