Spring/Spring MVC

Spring MVC - HTTP 헤더 조회

jddng 2022. 1. 28. 19:29
728x90
반응형

 

 

 

 

 

<참고 사이트>

 

Web on Servlet Stack (spring.io)

 

Web on Servlet Stack

Spring Web MVC is the original web framework built on the Servlet API and has been included in the Spring Framework from the very beginning. The formal name, “Spring Web MVC,” comes from the name of its source module (spring-webmvc), but it is more com

docs.spring.io

Web on Servlet Stack (spring.io)

 

Web on Servlet Stack

Spring Web MVC is the original web framework built on the Servlet API and has been included in the Spring Framework from the very beginning. The formal name, “Spring Web MVC,” comes from the name of its source module (spring-webmvc), but it is more com

docs.spring.io

 

HTTP 헤더 조회

 

 

  • 애노테이션 기반의 스프링 컨트롤러는 다양한 파라미터를 지원한다.
  • HttpServletRequest
            - HTTP 요청 헤더 정보가 파싱되어 있다.
  •  HttpServletResponse
            - HTTP 응답 메시지의 헤더 정보를 설정 할 수 있다.
  • HttpMethod
            - HTTP 요청 메서드를 읽어올 수 있다.
  • Locale
            - HTTP 요청 메시지의 언어 정보를 읽어올 수 있다.
  • @RequestHeader MultiValueMap<String, String> headerMap
            - 모든 HTTP 헤더를 MultiValueMap 형식으로 조회
  • @RequestHeader("host") String host
           - 특정 HTTP 헤더를 조회
           - 속성 : required, defaultValue
  • @CookieValue(value = "myCookie", required = false) String cookie
           - 특정 쿠키를 조회
           - 속성 : defaultValue

 

 

@Slf4j
@RestController
public class HeaderController{

    @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";
    }

}

 해당 헤더의 로그는 다음과 같다.

2022-01-28 19:19:00.099  INFO 1016 --- [nio-8080-exec-1] h.s.b.requestmapping.HeaderController    : request=org.apache.catalina.connector.RequestFacade@2f0aab20
2022-01-28 19:19:00.100  INFO 1016 --- [nio-8080-exec-1] h.s.b.requestmapping.HeaderController    : response=org.apache.catalina.connector.ResponseFacade@3b53de07
2022-01-28 19:19:00.100  INFO 1016 --- [nio-8080-exec-1] h.s.b.requestmapping.HeaderController    : httpMethod=GET
2022-01-28 19:19:00.100  INFO 1016 --- [nio-8080-exec-1] h.s.b.requestmapping.HeaderController    : locale=ko
2022-01-28 19:19:00.100  INFO 1016 --- [nio-8080-exec-1] h.s.b.requestmapping.HeaderController    : headerMap={host=[localhost:8080], connection=[keep-alive], cache-control=[max-age=0], sec-ch-ua=[" Not;A Brand";v="99", "Microsoft Edge";v="97", "Chromium";v="97"], sec-ch-ua-mobile=[?0], sec-ch-ua-platform=["Windows"], upgrade-insecure-requests=[1], user-agent=[Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36 Edg/97.0.1072.69], accept=[text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9], sec-fetch-site=[none], sec-fetch-mode=[navigate], sec-fetch-user=[?1], sec-fetch-dest=[document], accept-encoding=[gzip, deflate, br], accept-language=[ko,en;q=0.9,en-US;q=0.8]}
2022-01-28 19:19:00.100  INFO 1016 --- [nio-8080-exec-1] h.s.b.requestmapping.HeaderController    : header host=localhost:8080
2022-01-28 19:19:00.100  INFO 1016 --- [nio-8080-exec-1] h.s.b.requestmapping.HeaderController    : myCookie=null

 

 

 

 

728x90
반응형