HTTP 요청 메시지 - 단순텍스트 (@RequestBody,HttpEntity, RequestEntity)
- 📚 Spring/SpringMVC
- 2021. 10. 29. 22:38
HttpEntity
- HTTP header, body 정보를 편리하게 조회
- 메시지 바디 정보를 직접 조회
- 요청 파라미터를 조회하는 기능과 관계 없음(@RequestParam X , @ModelAttirbute X)
package hello.springmvc.basic.request;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.StreamUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
@Slf4j
@Controller
public class RequestBdyStringController {
@PostMapping("/request-body-string-v1")
public void requestBodyString(HttpServletRequest request, HttpServletResponse response) throws IOException {
ServletInputStream inputStream = request.getInputStream();
String messageBody = StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8);
log.info("messageBody={}", messageBody);
response.getWriter().write("ok");
}
@PostMapping("/request-body-string-v2")
public void requestBodyStringV2(InputStream inputStream, Writer responseWriter) throws IOException {
String messageBody = StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8);
log.info("messageBody={}", messageBody);
responseWriter.write("ok");
}
/**
* HttpEntity: HTTP header, body 정보를 편라하게 조회
* - 메시지 바디 정보를 직접 조회(@RequestParam X, @ModelAttribute X)
* - HttpMessageConverter 사용 -> StringHttpMessageConverter 적용
*
* 응답에서도 HttpEntity 사용 가능
* - 메시지 바디 정보 직접 반환(view 조회X)
* - HttpMessageConverter 사용 -> StringHttpMessageConverter 적용
*/
@PostMapping("/request-body-string-v3")
public HttpEntity<String> requestBodyStringV3(HttpEntity<String> httpEntity) throws IOException {
String messageBody = httpEntity.getBody();
log.info("messageBody={}", messageBody);
return new HttpEntity<>("ok");
}
@PostMapping("/request-body-string-v33")
public HttpEntity<String> requestBodyStringV33(RequestEntity<String> httpEntity) throws IOException {
String messageBody = httpEntity.getBody();
log.info("messageBody={}", messageBody);
//return new HttpEntity<>("ok");
return new ResponseEntity<>("ok", HttpStatus.CREATED);
}
}
Spring에서 더욱 간편한 방법을 제공하고 해당 기능은 실무에서 가장 많이 사용된다.
//실무에서 가장 많이 사용한다.
/**
* @RequestBody
* - 메시지 바디 정보를 직접 조회(@RequestParam X, @ModelAttribute X)
* - HttpMessageConverter 사용 -> StringHttpMessageConverter 적용
*
* @ResponseBody
* - 메시지 바디 정보 직접 반환(view 조회X)
* - HttpMessageConverter 사용 -> StringHttpMessageConverter 적용
*/
@PostMapping("/request-body-string-v4")
@ResponseBody
public String requestBodyStringV4(@RequestBody String messageBody) throws IOException {
log.info("messageBody={}", messageBody);
return "ok";
}
@RequestBody
- 'RequestBody'를 사용하면 HTTP 메시지 바디 정보를 편하게 조회할 수 있다.
- 참고로 헤더 정보가필요하다면 'HttpEntity'를 사용하거나 '@RequestHeader'를 사용하면 된다.
- 또한, 요청 파라미터를 조회하는 @RequestParam, @ModelAttribute 와는 전혀 관계 없다.
@ResponseBody
- '@Response'를 사용하면 응답결과를 HTTP 메시지 바디에 직접 담아서 전달할 수 있다.
- 물론 이 경우에도 view를 사용하지 않는다.
'📚 Spring > SpringMVC' 카테고리의 다른 글
[Spring] HTTP 응답 - HTTP API, 메시지 바디에 직접 입력 (0) | 2021.10.30 |
---|---|
HTTP 요청 메시지 - JSON (0) | 2021.10.29 |
[Sprng] 요청파라미터 > @ModelAttribute (0) | 2021.10.29 |
[Spring] 요청 파라미터 > @RequestParam (0) | 2021.10.29 |
[Spring] consumes 와 produces의 차이 (0) | 2021.10.26 |