

// 1. @Controller 어노테이션 방식
@Controller
public class HelloController {
@RequestMapping("/hello")
public String hello() {
return "hello";
}
}
// 2. Controller 인터페이스 방식
public class OldController implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) {
return new ModelAndView("hello");
}
}
// 3. HttpRequestHandler 인터페이스 방식
public class SimpleController implements HttpRequestHandler {
@Override
public void handleRequest(HttpServletRequest request,
HttpServletResponse response) {
// 처리 로직
}
}
여러 타입의 컨트롤러를 하나의 인터페이스로 처리 가능
// HandlerAdapter 인터페이스 (공통 인터페이스)
public interface HandlerAdapter {
boolean supports(Object handler); // 처리 가능 여부 판단(boolean)
ModelAndView handle( // 어떻게 처리할 것인지에 대한 로직
HttpServletRequest request,
HttpServletResponse response,
Object handler) throws Exception;
}
// Controller 구현 1 (HandlerAdapter를 implements)
public class RequestMappingHandlerAdapter implements HandlerAdapter {
@Override
public boolean supports(Object handler) {
return handler instanceof HandlerMethod; // @RequestMapping 메서드인지 확인
}
@Override
public ModelAndView handle(HttpServletRequest request,
HttpServletResponse response,
Object handler) {
// @Controller 방식으로 처리
return invokeHandlerMethod((HandlerMethod) handler, request, response);
}
}
// Controller 구현 2 (HandlerAdapter를 implements)
public class SimpleControllerHandlerAdapter implements HandlerAdapter {
@Override
public boolean supports(Object handler) {
return handler instanceof Controller;
}
@Override
public ModelAndView handle(HttpServletRequest request,
HttpServletResponse response,
Object handler) {
// Controller 인터페이스 방식으로 처리
return ((Controller) handler).handleRequest(request, response);
}
}
스프링 MVC의 프론트 컨트롤러

1. 요청
2. 핸들러 조회
3. 핸들러 실행
4. ModelAndView 반환
5. View 해석
6. View 렌더링
7. 응답
위의 절차는 SSR에서의 표준 흐름이며, CSR에서는 4번 이후의 흐름이 달라진다.
객체(Data)를 바로 반환하고, JSON 문자열로 변환한 뒤, 클라이언트에게 데이터만 바로 응답한다.
현대 스프링 MVC는 주로 4-6 과정은 사장되었으며, JSON 데이터만 반환하는 것이 일반적이다.