1️⃣ HTTP 요청 파라미터 - 쿼리 파라미터, HTML Form

https://www.inflearn.com/course/lecture?courseSlug=스프링-mvc-1&unitId=71218&tab=curriculum

서블릿에서 학습했던 HTTP 요청 데이터를 조회하는 방법을 다시 떠올려보고 ,이걸 스프링이 얼마나 효율적으로 바꿔주는지 알아보자.

HTTP요청 메시지를 클라이언트에서 서버로 보내는건 3가지 방법이 있다.

  1. GET 방식 - 쿼리 파라미터에 데이터를 포함해서 전달
  2. POST 방식 - HTML Form에 데이터 담아서 전달
  3. HTTP API - 메세지 바디에 데이터를 직접 담아서 요청

쿼리 파라미터 - 요청 파라미터 조회

여전히 request.getParameter()를 사용해 get과 Post로 전송된 내용들을 다 꺼낼 수 있다. 이 방식을 모두 요청 파라미터 조회라고 한다.

먼저 전에 서블릿에서 하던 방식이다

package hello.springmvc.basic.request;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.IOException;

@Slf4j
@Controller
public class RequestParamController {

    @RequestMapping("/request-param-v1")
    public void requestParamV1(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String username = request.getParameter("username");
        int age = Integer.parseInt(request.getParameter("age"));

        log.info("username={}, age = {}", username, age);

        response.getWriter().write("ok");
    }
}

Untitled

요청 파라미터에서 값을 잘 읽어왔다.

Post방식도 다를게 없다.

Untitled