애노테이션 기반의 스프링 컨트롤러는 다양한 파라미터를 지원하는데, 이 파라미터들로 HTTP 헤더 정보를 조회할 수 있음!
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.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;
@Slf4j //개발자는 편리하게 log라고 사용
@RestController
public class RequestHeaderController {
@RequestMapping("/headers")
public String headers(HttpServletRequest request,
HttpServletResponse response,
HttpMethod httpMethod, //Http 메서드 조회
Locale locale, //Locale 정보를 조회
@RequestHeader MultiValueMap<String, String> //map과 유사, 하나의 키에 여러 값 받을 수 있음
headerMap, //모든 http헤더를 MultiValueMap 형식으로 조회
@RequestHeader("host") String host, //특정 http 헤더를 조회
@CookieValue(value = "myCookie", required = false) //특정 쿠키를 조회
//required -> 필수 값 여부 / 기본값 -> defaultValue
String cookie() {
log.info("request={}", request);
log.info("response={}", response);
log.info("httpMethod={}", httpMethod);
log.info("locale={}", locale);
log.info("headerMap={}", headerMap);
log.info("header host={}", host);
log.info("myCookie={}", cookie);
return "ok";
}
}
클라이언트에서 서버로 요청 데이터를 전달할 때
쿼리파라미터, HTML form일때
HttpServletRequest의 request.getParameter()를 사용하면 다음 두가지 요청 파라미터 조회 가능
→ 요청 파라미터 조회 라고 부름
RequestParamController