그렇다면 HTTP 메시지 컨버터는 스프링MVC 어디쯤에서 사용되는 것일까?
모든 비밀은 애노테이션 기반의 컨트롤러 , @RequestMapping 을 처리하는 핸들러 어댑터인
RequestMappingHandlerAdapter에 있다.

ArgumentResolver
애노테이션 기반의 컨트롤러는 매우 다양한 파라미터를 사용할 수 있었다.
HttpServletRequest, Model은 물론 @RequestParam, ModelAttribute같은 애노테이션 그리고
@RequestBody, HttpEntity 같은 HTTP 메시지를 처리하는 부분까지 매우 큰 유연함을 보여주었다.
이렇게 ㅍ파라미터를 유연하게 처리할 수 있는 이유가 바로 ArgumentResolver 덕분이다.
애노테이션 기반 컨트롤러를 처리하는 RequestMappingHandlerAdapter는 ArgumentResolver를 호출해서
컨트롤러(핸들러)가 필요로 하는 다양한 파라미터의 값을 생성한다. 그리고 파라미터의 값이 모두 준비되면
컨트롤러를 호출하면서 값을 넘겨준다.
스프링은 30개가 넘는 ArgumentResolver를 기본으로 제공한다.
/*
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.method.support;
import org.springframework.core.MethodParameter;
import org.springframework.lang.Nullable;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
/**
* Strategy interface for resolving method parameters into argument values in
* the context of a given request.
*
* @author Arjen Poutsma
* @since 3.1
* @see HandlerMethodReturnValueHandler
*/
public interface HandlerMethodArgumentResolver {
/**
* Whether the given {@linkplain MethodParameter method parameter} is
* supported by this resolver.
* @param parameter the method parameter to check
* @return {@code true} if this resolver supports the supplied parameter;
* {@code false} otherwise
*/
boolean supportsParameter(MethodParameter parameter);
/**
* Resolves a method parameter into an argument value from a given request.
* A {@link ModelAndViewContainer} provides access to the model for the
* request. A {@link WebDataBinderFactory} provides a way to create
* a {@link WebDataBinder} instance when needed for data binding and
* type conversion purposes.
* @param parameter the method parameter to resolve. This parameter must
* have previously been passed to {@link #supportsParameter} which must
* have returned {@code true}.
* @param mavContainer the ModelAndViewContainer for the current request
* @param webRequest the current request
* @param binderFactory a factory for creating {@link WebDataBinder} instances
* @return the resolved argument value, or {@code null} if not resolvable
* @throws Exception in case of errors with the preparation of argument values
*/
@Nullable
Object resolveArgument(MethodParameter parameter, @Nullable ModelAndViewContainer mavContainer,
NativeWebRequest webRequest, @Nullable WebDataBinderFactory binderFactory) throws Exception;
}
정확히는 HandlerMethodArgumentResolver인데 줄여서 ArgumentResolver 라고 부름
동작방식
ArgumentResolver의 supportParameter()를 호출해서 해당 파라미터를 지원하는지 체크하고, 지원하면 resolveArgument()를 호출해서 실제 객체를 생성한다. 그리고 이렇게 생성된 객체가 컨트롤러 호출시 넘어가는 것이다.
그리고 원한다면 사용자가 인터페이스를 확장해서 원하는 ArgumentResolver를 만들 수도 있다.
ReturnValueHandler
HandlerMethodResutrnValueHandler를 줄여서 ReturnValueHandler 라 부른다.
ArgumentResolver와 비슷한데, 이것은 응답 값을 변환하고 처리한다.
컨트롤러에서 String으로 뷰 이름을 반환해도, 동작하는 이유가 바로 ReturnValueHandler 덕분이다.

HTTP 메시지 컨버터는 어디있을까?
HTTP 메시지 컨버터를 사용하는 @RequestBody도 컨트롤러가 필요로 하는 파라미터 값에 사용된다.
@ResponseBody의 경우도 컨트롤러의 반환 값을 이용한다.
요청의 경우 @RequestBody 를 처리한느 ArgumentResolver가 있고, HttpEntity를 처리하는 ArgumentResolver가 있다.
이 ArgumentResolver들이 HTTP 메시지 컨버터를 사용해서 필요한 객체를 생성하는 것이다.
응답의 경우 @ResponseBody와 HttpEntity를 처리하는 ReturnValueHandler가 있다. 그리고 여기에서 HTTP 메시지
컨버터를 호출해서 응답 결과를 만든다.
스프링 MVC는 @RequestBody @ResponseBody가 있으면
RequestResponseBodyMethodProcessor(ArgumentResolver)
HttpEntity가 있으면 HttpEntityMethodProcessor(ArgumentResolver)를 사용한다.
'웹프로그래밍 > 스프링 MVC' 카테고리의 다른 글
| 52. 요구사항 분석 (0) | 2022.04.14 |
|---|---|
| 51. 프로젝트 생성 (0) | 2022.04.14 |
| 49. HTTP 메시지 컨버터 (0) | 2022.04.08 |
| 48. HTTP 응답 - HTTP API, 메시지 바디에 직접 입력 (0) | 2022.04.08 |
| 47. 응답 - 정적 리소스, 뷰 템플릿 (0) | 2022.04.07 |