반응형
250x250
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- Blazor
- 틱택토구현
- 제로초
- 자바스크립트틱택토
- c#
- 인프런자바스크립트
- 자바스크립트함수
- 고차함수
- 인터넷프로토콜
- .NET
- EntityFramework
- 인프런
- 인프런인강
- 자바스크립트
- 자바스크립트recude
- 이벤트리스너
- 비주얼스튜디오
- NPM
- slice
- 인프런강좌
- 인프런강의
- 코딩
- sort
- 객체리터럴
- HTTP
- 객체의비교
- 자바스크립트파라미터
- 인프런무료강좌
- 자바스크립트객체리터럴
- 콜백함수
Archives
- Today
- Total
샐님은 개발중
6. HTTP 요청 메시지 - 단순 텍스트 본문
728x90
반응형
HTTP message body 에 데이터를 직접 담아서 요청
-http api 에서 주로 사용, json, xml, text
-데이터 형식은 주로 json 사용
- post, put, patch
요청 파타미터와 다르게 http 메시지 바디를 통해 데이터가 직접 넘어오는 경우는 @RequestParam , @ModelAttribute 를 사용할 수 없다. (물론 HTML Form 형식으로 전달되는 경우는 요청 파라미터로 인정된다.)
package hello.springmvc.basic.request;
import jakarta.servlet.ServletInputStream;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
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 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 {
//InputStream(Reader): HTTP 요청 메시지 바디의 내용을 직접 조회
ServletInputStream inputStream = request.getInputStream();
String messageBody = StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8);
log.info("message body ={}",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("message body ={}",messageBody);
responseWriter.write("ok");
}
@PostMapping("/request-body-string-v3")
public HttpEntity<String> requestBodyStringV3(HttpEntity<String> httpEntity) throws IOException {
//HttpEntity: HTTP header, body 정보를 편리하게 조회
String body = httpEntity.getBody();
log.info("message body ={}",body);
return new HttpEntity<>("ok");
}
//@RequestBody 를 사용하면 HTTP 메시지 바디 정보를 편리하게 조회할 수 있다.
@ResponseBody
@PostMapping("/request-body-string-v4")
public String requestBodyStringV4(@RequestBody String messageBody) throws IOException {
log.info("message body ={}",messageBody);
return ("ok");
}
}
728x90
반응형
'스프링 MVC 1편 -인프런 김영한 > 섹션6-스프링MVC - 기본 기능' 카테고리의 다른 글
8. HTTP 응답 - 정적 리소스, 뷰 템플릿 (0) | 2023.07.10 |
---|---|
7. HTTP 요청 메시지 - JSON (0) | 2023.07.10 |
5. HTTP 요청 파라미터 - @ModelAttribute (0) | 2023.07.10 |
4. HTTP 요청 파라미터 - 쿼리 파라미터, HTML Form (0) | 2023.07.10 |
3. HTTP 요청 - 기본,헤더 조회 (0) | 2023.07.10 |