
@RequestBody, @HttpEntity(RequestEntity)@ResponseBody, @HttpEntity(ResponseEntity)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;
}
canRead(), canWrite() : 메시지 컨버터가 해당 클래스, 미디어 타입을 지원하는지 체크read(), write() : 메시지 컨버터를 통해 메시지를 읽고 쓰는 기능(숫자는 우선 순위이며, 이외에 몇 가지 더 있으나 생략)
0 = ByteArrayHttpMessageConverter
1 = StringHttpMessageConverter
2 = MappingJackson2HttpMessageConverter
byte[] 데이터를 처리byte[], 미디어 타입 : */*@RequestBody byte[] data@ResponseBody return byte[], 쓰기 미디어 타입 application/octet-stream → 응답 헤더에 해당 값이 들어감String 문자로 데이터를 처리String, 미디어 타입 : */*@RequestBody String data@ResponseBody return "ok", 쓰기 미디어 타입 text/plainapplication/jsonHashMap, 미디어 타입 : application/json 관련@RequestBody HelloData data@ResponseBody return helloData, 쓰기 미디어 타입 application/json 관련content-type: application/json
@RequestMapping
void hello(@RequestBody String data) {}
content-type: application/json
@RequestMapping
void hello(@RequestBody HelloData data) {}
content-type: text/html
@RequestMapping
void hello(@RequestBody HelloData data) {}
참고 Reference