상세 컨텐츠

본문 제목

Spring MVC @CookieValue

프레임워크/Spring Boot

by 최승호 2022. 5. 24. 12:00

본문

https://docs.spring.io/spring-framework/docs/current/reference/html/web.html#mvc-ann-cookievalue

 

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

@CookieValue 어노테이션은

컨트롤러의 메소드에서 쿠키값을 바인딩할 수 있다.

 

@Slf4j
@RestController
public class CookieValueController {
 
    // 쿠키 생성하기
    @GetMapping("/createCookieValue")
    public void createCookieValue(HttpServletResponse response) {
        Cookie cookie = new Cookie("cookie", "HelloWorld");
        response.addCookie(cookie);
    }

    // 쿠키값 읽기
    @GetMapping("/cookieValue")
    public void cookieValue(@CookieValue("cookie") String cookie) {
        log.info("cookie : {}", cookie);
    }
}

 

실행결과

 

2022-05-20 17:53:50.551 INFO 26404 --- [nio-8080-exec-2] c.e.s.controller.CookieValueController : createCookieValue
2022-05-20 17:53:50.551 INFO 26404 --- [nio-8080-exec-2] c.e.s.controller.CookieValueController : 쿠키 생성 완료
2022-05-20 17:53:57.409 INFO 26404 --- [nio-8080-exec-3] c.e.s.controller.CookieValueController : cookie : HelloWorld

 

관련글 더보기

댓글 영역