목록Spring (36)
Rootable의 개발일기
📌 역할 1. HTTP 메시지 바디의 내용을 우리가 읽을 수 있도록 객체 또는 문자로 변환해 준다.2. HTTP 메시지 바디에 우리가 생성한 데이터를 적을 수 있도록 한다.3. HTTP 메시지 컨버터는 지원하는 클래스 타입, 미디어 타입에 따라 메시지 컨버터가 선택된다. HTTP Body의 데이터를 직접 읽거나 생성할 때 사용 📌 사용 원리 @ResponseBody를 사용하면 HttpMessageConverter가 동작한다. 이 경우, 뷰 리졸버가 동작하지 않는다. 따라서, 화면을 렌더링 하지 않고 HTTP 바디의 데이터를 읽거나 쓸 수 있도록 변환하는 작업을 한다. 예를 들어 아래 그림처럼 String 타입 데이터를 받은 후 String 타입 데이터를 내려주는 경우, HttpMessageConver..
HTTP에서 요청 데이터를 전달할 때 주로 3가지 방식을 사용한다. 1. GET 쿼리 파라미터 전송 2. POST HTML Form 전송 3. HTTP Body에 직접 담아서 전송 - HTTP API 요청 데이터를 처리하는 방법을 하나씩 알아보고 각 방식에 맞게 적절하게 선택하자. 📌 GET 쿼리 파라미터 및 POST HTML Form - 요청 파라미터 처리 🔎 서블릿 HttpServletRequest.getParameter를 통해 요청 파라미터를 다룰 수 있다. 이 방식은 GET 쿼리 파라미터 전송 방식과 POST HTML Form 전송 방식 둘 다 형식이 같으므로 구분 없이 조회 가능하다. 🔎 @RequestParam HttpServletRequest와 같은 역할 즉, 요청 파라미터 값을 받아..
📌 요청 URL 및 메서드 매핑 🔎 @RequestMapping "/hello-basic"이라는 요청 URL이 오면 응답 @RequestMapping은 HTTP 메서드를 모두 허용함 🔎 축약형 애노테이션 특정 메서드 요청에만 응답 스프링은 @GetMapping뿐만 아니라 HTTP 메서드 축약형 애노테이션인 @PostMapping, @PutMapping 등 모두 지원함 🔎 PathVariable 경로 변수 특정 사용자 또는 특정 게시물 등을 표현하기 위해서는 URL 경로에 동적 경로가 들어가게 된다. 예를 들어, "/members/1"는 1번 회원에 대한 페이지를 말한다. 위 URL처럼 '1'이라는 경로는 계속 변경되기 때문에 이 값마다 처리하는 컨트롤러를 만드는 것은 매우 비효율적이다. 그래서 스프링은 ..
📌 @RequestParam 단일 데이터를 받기 위해 사용 클라이언트 요청의 Content-Type이 "application/json"이든 "application/x-www-form-encoded"이든 "multipart/form-data"이든 상관 없이 동일하게 key에 매칭되는 하나의 필드 값을 받을 수 있다. URL에서 파라미터를 추출하기 때문에 HTTP API처럼 직접 HTTP 바디에 데이터를 담는 경우에는 사용 불가 요청 파라미터 이름과 변수 이름이 같으면, @RequestParam의 name 값이 없어도 된다. 위 경우처럼 이름이 같은 상황에서 String, int, Integer 등 단순 타입이면 @RequestParam 생략 가능 @RequestParam 생략하면 Spring MVC..