애노테이션 기반의 스프링 컨트롤러는 다양한 파라미터를 지원한다. 이번 시간에는 HTTP 헤더 정보를 조회하는 방법을 알아보자.
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;
- import 되는 부분
@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";
}
}
[ 결과 ]
2023-03-30 20:26:23.063 INFO 13004 --- [nio-8080-exec-1] h.s.b.request.RequestHeaderController : request=org.apache.catalina.connector.RequestFacade@8fbcd0b
2023-03-30 20:26:23.071 INFO 13004 --- [nio-8080-exec-1] h.s.b.request.RequestHeaderController : response=org.apache.catalina.connector.ResponseFacade@1641d77d
2023-03-30 20:26:23.071 INFO 13004 --- [nio-8080-exec-1] h.s.b.request.RequestHeaderController : httpMethod=GET
2023-03-30 20:26:23.071 INFO 13004 --- [nio-8080-exec-1] h.s.b.request.RequestHeaderController : locale=ko_KR
2023-03-30 20:26:23.071 INFO 13004 --- [nio-8080-exec-1] h.s.b.request.RequestHeaderController : headerMap={host=[localhost:8080], connection=[keep-alive], sec-ch-ua=["Google Chrome";v="111", "Not(A:Brand";v="8", "Chromium";v="111"], 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/111.0.0.0 Safari/537.36], accept=[text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7], sec-fetch-site=[none], sec-fetch-mode=[navigate], sec-fetch-user=[?1], sec-fetch-dest=[document], accept-encoding=[gzip, deflate, br], accept-language=[ko-KR,ko;q=0.9,en-US;q=0.8,en;q=0.7]}
2023-03-30 20:26:23.071 INFO 13004 --- [nio-8080-exec-1] h.s.b.request.RequestHeaderController : header host=localhost:8080
2023-03-30 20:26:23.071 INFO 13004 --- [nio-8080-exec-1] h.s.b.request.RequestHeaderController : myCookie=null
- HttpMethod : HTTP 메서드를 조회한다. org.springframework.http.HttpMethod
- Locale : : Locale 정보를 조회한다.
- @RequestHeader MultiValueMap headerMap : 모든 HTTP 헤더를 MultiValueMap 형식으로 조회한다.
- @RequestHeader("host") String host : 특정 HTTP 헤더를 조회한다
속성 : 필수 값 여부: required , 기본 값: defaultValue - MultiValueMap : MAP과 유사한데, 하나의 키에 여러 값을 받을 수 있다
HTTP header, HTTP 쿼리 파라미터와 같이 하나의 키에 여러 값을 받을 때 사용한다
keyA=value1&keyA=value2
@Conroller 의 사용 가능한 파라미터 목록은 다음 공식 메뉴얼에서 확인할 수 있다
> https://docs.spring.io/spring-framework/docs/current/reference/html/web.html#mvc-annarguments
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 commonl
docs.spring.io
@Conroller 의 사용 가능한 응답 값 목록은 다음 공식 메뉴얼에서 확인할 수 있다.
> https://docs.spring.io/spring-framework/docs/current/reference/html/web.html#mvc-annreturn-types
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 commonl
docs.spring.io
'spring' 카테고리의 다른 글
@RequestParam (0) | 2023.03.30 |
---|---|
HTTP 요청 파라미터 (0) | 2023.03.30 |
요청 매핑 - 연습 (0) | 2023.03.27 |
요청 매핑 (0) | 2023.03.27 |
로깅 (0) | 2023.03.27 |