📩 HTTP Message Body(요청)
✅ 왜 이걸 배워야 할까?
@RequestParam, @ModelAttribute는 쿼리 파라미터나 폼 데이터를 처리함✉️ HTTP Message 구조

🔧 HTTP Request, Response 예시

📚 TEXT
1. HttpServletRequest 사용
@Slf4j
@Controller
public class RequestBodyStringController {
@PostMapping("/v1/request-body-text")
public void requestBodyTextV1(
HttpServletRequest request,
HttpServletResponse response
) throws IOException {
ServletInputStream inputStream = request.getInputStream();
String bodyText = StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8);
response.getWriter().write("response = " + bodyText);
}
}
📌 Postman 설정:
Content-Type: text/plain2. InputStream / Writer 파라미터 직접 사용
@PostMapping("/v2/request-body-text")
public void requestBodyTextV2(
InputStream inputStream,
Writer responseWriter
) throws IOException {
String body = StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8);
responseWriter.write("response = " + bodyText);
}
→ ServletRequest 없이도 Stream으로 직접 처리 가능
3. HttpEntity 사용 (권장)
@PostMapping("/v3/request-body-text")
public HttpEntity<String> requestBodyTextV3(HttpEntity<String> httpEntity) {
// HttpMessageConverter가 동작해서 아래 코드가 동작하게됨
String body = httpEntity.getBody();
return new HttpEntity<>("response = " + body); // 매개변수 = Body Message
}
→ HttpEntity<T>는 요청 바디를 자동으로 변환해 줌
→ 타입이 String이면 text/plain, 객체면 application/json 등 자동 처리
→ 응답도 함께 반환할 수 있음