본문 바로가기
개발 개념

[Java] Http 헤더 정보

by Yikanghee 2022. 1. 30.

HTTP 헤더 정보 받기

  • 스프링은 HTTP 헤더 정보를 받을 수 있는 여러 동작을 지원한다
  • 아래와 같이 메서드를 추가하여 LOG를 찍어보면 다음과 같은 결과를 얻을 수 있다
@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-30 19:29:12.326 INFO 11152 --- [nio-8081-exec-3] h.s.b.request.RequestHeaderController : request=org.apache.catalina.connector.RequestFacade@66296 a 7 b 2022-01-30 19:29:12.327 INFO 11152 --- [nio-8081-exec-3] h.s.b.request.RequestHeaderController : response=org.apache.catalina.connector.ResponseFacade@2 f163755 2022-01-30 19:29:12.327 INFO 11152 --- [nio-8081-exec-3] h.s.b.request.RequestHeaderController : httpMethod=DELETE 2022-01-30 19:29:12.327 INFO 11152 --- [nio-8081-exec-3] h.s.b.request.RequestHeaderController : locale=ko_KR 2022-01-30 19:29:12.327 INFO 11152 --- [nio-8081-exec-3] h.s.b.request.RequestHeaderController : headerMap={content-type=[text/plain], user-agent=[PostmanRuntime/7.29.0], accept=[*/*], cache-control=[no-cache], postman-token=[57 b4 ae9 b-ed75-430f-bf6c-9 e 7 d28 c64 e3 d], host=[localhost:8081], accept-encoding=[gzip, deflate, br], connection=[keep-alive], content-length=[32]} 2022-01-30 19:29:12.327 INFO 11152 --- [nio-8081-exec-3] h.s.b.request.RequestHeaderController : header host=localhost:8081 2022-01-30 19:29:12.327 INFO 11152 --- [nio-8081-exec-3] h.s.b.request.RequestHeaderController : myCookie=null
  1. Requeest 정보
  2. Reponse 정보
  3. httpMethod 정보
  4. 가장 많이 사용된 언어 정보
  5. 헤더 정보를 MultiValueMap에 넣어서 조회
  6. host정보를 매개변수에 담아 조회
  7. 쿠키정보가 있으면 조회

https://docs.spring.io/spring-framework/docs/current/reference/html/web.html#mvc-annreturn https://docs.spring.io/spring-framework/docs/current/reference/html/web.html#mvc-annarg

 

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 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

 

댓글