728x90
@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";
}
}
- HttpServletRequest : HttpServletRequest
- HttpServletResponse : HttpServletResponse
- HttpMethod : HTTP 메서드를 조회한다.
- org.springframework.http.HttpMethod Locale : Locale 정보를 조회한다.
- @RequestHeader MultiValueMap headerMap
- 모든 HTTP 헤더를 MultiValueMap 형식으로 조회한다.
- @RequestHeader("host") String host
- 특정 HTTP 헤더를 조회한다.
- 속성
- 필수 값 여부: required
- 기본 값 속성: defaultValue
- @CookieValue(value = "myCookie", required = false) String cookie
- 특정 쿠키를 조회한다.
- 속성
- 필수 값 여부: required
- 기본 값: defaultValue
참고
- MultiValueMap
- MAP과 유사한데, 하나의 키에 여러 값을 받을 수 있다.
- HTTP header, HTTP 쿼리 파라미터와 같이 하나의 키에 여러 값을 받을 때 사용한다.
- keyA=value1&keyA=value2
- @Conroller의 사용 가능한 파라미터 목록은 다음 공식 매뉴얼에서 확인할 수 있다.
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
- @Conroller의 사용 가능한 응답 값 목록은 다음 공식 매뉴얼에서 확인할 수 있다.
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
728x90
'스프링 MVC 1편 (백엔드 웹 개발 핵심 기술)' 카테고리의 다른 글
| Ch06. 스프링 MVC(기본 기능) - HTTP 응답 (0) | 2022.03.07 |
|---|---|
| Ch06. 스프링 MVC(기본 기능) - HTTP 요청 파라미터 (0) | 2022.03.07 |
| Ch06. 스프링 MVC(기본 기능) - 요청 매핑(API 예시) (0) | 2022.03.06 |
| Ch06. 스프링 MVC(기본 기능) - 요청 매핑 (0) | 2022.03.06 |
| Ch06. 스프링 MVC(기본 기능) - 로깅 간단히 알아보기 (0) | 2022.03.06 |