on
HTTP 요청 메시지 (단순 텍스트)
HTTP 요청 메시지 (단순 텍스트)
728x90
import lombok.extern.slf4j.Slf4j; import org.springframework.http.HttpEntity; 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 RequestBodyStringController { @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"); } // HTTP header, body 정보를 편리하게 조회 // 요청 파라미터를 조회하는 기능과 관계없음 @PostMapping("/request-body-string-v3") public HttpEntity requestBodyStringV3(HttpEntity httpEntity) throws IOException { String messageBody = httpEntity.getBody(); log.info("messageBody={}", messageBody); return new HttpEntity<>("ok"); } @ResponseBody @PostMapping("/request-body-string-v4") public String requestBodyStringV4(@RequestBody String messageBody){ log.info("messageBody={}", messageBody); return "ok"; } }
728x90
from http://arch1tect.tistory.com/120 by ccl(A) rewrite - 2021-09-09 02:27:39