https://www.inflearn.com/course/lecture?courseSlug=스프링-mvc-1&unitId=71218&tab=curriculum
서블릿에서 학습했던 HTTP 요청 데이터를 조회하는 방법을 다시 떠올려보고 ,이걸 스프링이 얼마나 효율적으로 바꿔주는지 알아보자.
HTTP요청 메시지를 클라이언트에서 서버로 보내는건 3가지 방법이 있다.
여전히 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");
}
}
요청 파라미터에서 값을 잘 읽어왔다.
Post방식도 다를게 없다.