본문 바로가기
728x90
반응형

스프링/스프링 웹53

[Spring] HTTP 요청 메시지 - JSON 이번에는 HTTP API에서 주로 사용하는 JSON 데이터 형식을 조회해본다. 기존 서블릿에서 사용했던 방식과 비슷하게 시작해본다. RequestBodyJsonController package hello.springmvc.basic.request; import com.fasterxml.jackson.databind.ObjectMapper; import hello.springmvc.basic.HelloData; import lombok.extern.slf4j.Slf4j; import org.springframework.http.HttpEntity; import org.springframework.stereotype.Controller; import org.springframework.util.StreamU.. 2023. 6. 12.
[Spring] HTTP 요청 메시지 - 단순 텍스트 HTTP message body는 데이터를 직접 담아서 요청을 보내는 것이다. - HTTP API에서 주로 사용하고, JSON, XML, TEXT 등이 있다. - 데이터 형식은 주로 JSON을 사용한다. - POST, PUT, PATCH가 있다. 요청 파라미터와 다르게, HTTP 메시지 바디를 통해 데이터가 직접 넘어오는 경우는 @RequestParam, @ModelAttribute를 사용할 수 없다. 물론 HTML Form 형식으로 전달되는 경우는 요청 파라미터로 인정 된다. 가장 단순한 텍스트 메시지를 HTTP 메시지 바디에 담아서 전송하고 읽어본다. HTTP 메시지 바디의 데이터를 InputStream 을 사용해서 직접 읽을 수 있다. RequestBodyStringController package.. 2023. 6. 12.
[Spring] HTTP 요청 파라미터 - @ModelAttribute @ResponseBody @RequestMapping("/request-param-v3") public String requestParamV3( @RequestParam String username, @RequestParam int age ) { log.info("username={}, age={}", username, age); HelloData data = new HelloData(); data.setUserName(username); data.setAge(age); return "ok"; } 실제 개발을 하면 요청 파라미터를 받아서 필요한 객체를 만들고 값을 넣어주어야 한다. 보통은 위 코드처럼 데이터를 넣어주는 작업을 해야한다. 스프링은 위와 같은 과정을 자동화해주는 @ModelAttribute .. 2023. 6. 12.
[Spring] HTTP 요청 파라미터 - @RequestParam requestParamV2 /** * @RequestParam 사용 * - 파라미터 이름으로 바인딩 * @ResponseBody 추가 * - View 조회를 무시하고, HTTP message body에 직접 해당 내용 입력 */ @ResponseBody // @RestController 랑 같은 효과를 봄 @RequestMapping("/request-param-v2") public String requestParamV2( @RequestParam("username") String memberName, @RequestParam("age") int memberAge ) { log.info("username={}, age={}", memberName, memberAge); return "ok"; } @Req.. 2023. 6. 9.
[Spring] HTTP 요청 파라미터 - 쿼리 파라미터, HTML Form 클라이언트에서 서버로 요청 데이터를 전달할 때는 주로 다음 3가지 방법을 사용한다. 1) GET - 쿼리 파라미터 - /url?username=hello&age=20 - 메시지 바디 없이, URL의 쿼리 파라미터에 데이터를 포함해서 전달 - 예) 검색, 필터, 페이징 등에서 많이 사용하는 방식 2) POST - HTML Form - content-type : application/x-www-form-urlencoded - 메시지 바디에 쿼리 파라미터 형식으로 전달 username=hello&age=20 - 예) 회원 가입, 상품 주문, HTML Form 사용 3) HTTP message body에 데이터를 직접 담아서 요청 - HTTP API 에서 주로 사용, JSON, XML, TEXT - 데이터 형식.. 2023. 6. 9.
[Spring] HTTP 요청 - 기본, 헤더 조회 ( MultiValueMap 등) 애노테이션 기반의 스프링 컨트롤러는 다양한 파라미터를 지원한다. RequestHeaderController package hello.springmvc.basic.request; import lombok.extern.slf4j.Slf4j; import org.springframework.http.HttpMethod; import org.springframework.util.MultiValueMap; import org.springframework.web.bind.annotation.CookieValue; import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.Reque.. 2023. 6. 8.
728x90
반응형