오봉이와 함께하는 개발 블로그

스프링 MVC 1 - HTTP 요청 기본, 헤더 조회 본문

BE/Spring

스프링 MVC 1 - HTTP 요청 기본, 헤더 조회

오봉봉이 2022. 8. 13. 21:42
728x90

HTTP 요청 - 기본, 헤더 조회

HTTP 헤더 정보를 조회하는 방법을 알아보자.

RequestHeaderController

@Slf4j
@RestController
public class RequestHeaderController {

    @RequestMapping("/headers")
    public String headers(HttpServletRequest request, HttpServletResponse response, HttpMethod httpMethod, Locale locale,
                          @RequestHeader MultiValueMap<String, String> headerMap, @RequestHeader("host") String host,
                          @CookieValue(value = "myCookie", required = false) 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";
    }
}
  • HttpMethod : HTTP 메서드를 조회한다. (org.springframework.http.HttpMethod)
  • Locale : Locale 정보를 조회한다.
  • @RequestHeader MultiValueMap<String, String> headerMap : 모든 HTTP 헤더를 MultiValueMap 형식으로 조회한다.
    • MultiValueMap
      • Map과 유사한데, 하나의 키에 여러 값을 받을 수 있다.
      • HTTP header, HTTP 쿼리 파라미터와 같이 하나의 키에 여러 값을 반환 받을 때 사용한다.
  • @RequestHeader("host") String host : 특정 헤더를 조회한다.
    • 속성
      • 필수 값 여부 : required
      • 기본 값 속성 : defaultValue
  • @CookieValue(value = "myCookie", required = false) String cookie : 특정 쿠키를 조회한다.
    • 속성
      • 필수 값 여부 : required
      • 기본 값 : defaultValue
출처 : 인프런 김영한 지식 공유자님 강의 - 스프링 MVC 1편 백엔드 웹 개발 핵심 기술
728x90
Comments