@Slf4j
@RestController
public class RequestHeaderController {
@RequestMapping("/headers")
public String headers(
HttpServletRequest request,
HttpServletResponse response,
HttpMethod httpMethod,
Locale locale,
// header 한 번에 다 받기
@RequestHeader MultiValueMap<String, String> headerMap,
// 필요한 header를 key 값으로 해서 받기
@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";
}
}
class Example {
void example() {
MultiValueMap<String, String> map = new LinkedMultiValueMap();
map.add("keyA", "value1");
map.add("keyA", "value2");
// [value1, value2]
List<String> values = map.get("keyA");
}
}