> For the complete documentation index, see [llms.txt](https://heunnajo.gitbook.io/mvc/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://heunnajo.gitbook.io/mvc/5.-mvc/undefined.md).

# 핸들러 매핑과 핸들러 어댑터

1. 핸들러 맵핑으로 핸들러(컨트롤러) 조회\
   핸들러 매핑에서 이 컨트롤러를 찾을 수 있어야 한다.\
   **어떤 요청(url)이 들어왔을 때 이를 처리할 수 있는 컨트롤러를 찾아야한다.** 그렇기 때문에 **해당 url 요청이 들어왔을 때 실행할 메서드를 구현**하고, 그 **상위 클래스에 @Controller 애노테이션을 붙여준다.**
2. 핸들러 어댑터 조회\
   핸들러 매핑을 통해서 찾은 핸들러를 실행할 수 있는 핸들러 어댑터가 필요하다.
3. 핸들러 어댑터 실행

HttpRequestHandler를 상속받는 MyHttpRequestHandler로 핸들러 맵핑과 핸들러 어댑터 동작을 이해해보자!

```java
package hello.servlet.web.springmvc.old;

import org.springframework.stereotype.Component;
import org.springframework.web.HttpRequestHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Component("/springmvc/request-handler")
public class MyHttpRequestHandler implements HttpRequestHandler {
    @Override
    public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("MyHttpRequestHandler.handleRequest");
    }
}
```

1. **핸들러 맵핑으로 핸들러 조회**\
   **HandlerMapping**\
   0 = RequestMappingHandlerMapping : 애노테이션 기반의 컨트롤러인 @RequestMapping에서 사용\
   **1 = BeanNameUrlHandlerMapping : 스프링 빈의 이름으로 핸들러를 찾는다.**\
   **MyHttpRequestHandler 를 반환한다.**
2. 핸들러 어댑터 조회\
   HandlerAdapter\
   0 = RequestMappingHandlerAdapter : 애노테이션 기반의 컨트롤러인 @RequestMapping에서 사용\
   **1 = HttpRequestHandlerAdapter : HttpRequestHandler 처리**\
   **HttpRequestHandler를 상속받아 구현했기 때문에 해당된다!**\
   2 = SimpleControllerHandlerAdapter : Controller 인터페이스(애노테이션X, 과거에 사용) 처리
3. 핸들러 어댑터 실행\
   디스패처 서블릿이 조회한 HttpRequestHandlerAdapter 를 실행하면서 핸들러 정보도 함께 넘겨준다.\
   supports-handle 구조를 확인하자!\
   handle함수 내에 handleRequest를 실행한다! => MyHttpRequestHandler 상속받으면서 오버라이딩한 handleRequest가 실행된다!

```java
public class HttpRequestHandlerAdapter implements HandlerAdapter {

	@Override
	public boolean supports(Object handler) {
		return (handler instanceof HttpRequestHandler);
	}

	@Override
	@Nullable
	public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {

		((HttpRequestHandler) handler).handleRequest(request, response);
		return null;
	}
```

@RequestMapping

조금 뒤에서 설명하겠지만, 가장 우선순위가 높은 **핸들러 매핑**과 ***핸들러 어댑터***&#xB294; **RequestMappingHandlerMapping** ,\
\&#xNAN;***RequestMappingHandlerAdapter*** 이다.\
\&#xNAN;**@RequestMapping 의 앞글자를 따서 만든 이름인데, 이것이 바로 지금 스프링에서 주로 사용하는 애노테이션 기반의 컨트롤러를 지원하는 매핑과 어댑터**이다. 실무에서는 99.9% 이 방식의 컨트롤러를 사용한다.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://heunnajo.gitbook.io/mvc/5.-mvc/undefined.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
