이번에는 이전부터 계속 언급이 되었던 HTTP 메시지 컨버터에 대해 알아보려고 한다.

스프링 MVC는 메시지 바디에 맞춰서 알맞게 메시지 컨버터를 적용한다.

메시지 컨버터 구조

메시지 컨버터는 인터페이스로 이루어져있다.

package org.springframework.http.converter;

public interface HttpMessageConverter<T> {
	boolean canRead(Class<?> clazz, @Nullable MediaType mediaType);
	boolean canWrite(Class<?> clazz, @Nullable MediaType mediaType);

	List<MediaType> getSupportedMediaTypes();

	T read(Class<? extends T> clazz, HttpInputMessage inputMessage)
		throws IOException, HttpMessageNotReadableException;

	void write(T t, @Nullable MediaType contentType, HttpOutputMessage outputMessage)
		throws IOException, HttpMessageNotWritableException;
}

동작

기본적으로 HTTP 메시지 컨버터는 요청과 응답에 둘 다 사용된다.

종류

각 클래스 타입, 미디어 타입에 알맞는 메시지 컨버터가 선택되어 처리하게 된다.

그렇다면 이러한 메시지 컨버터는 어디서 실행이 되는 걸까?

Untitled

그 위치는 바로 핸들러 어댑터 쪽에 있다.