gpt4 book ai didi

java - Spring MVC 的 Cookie 问题

转载 作者:行者123 更新时间:2023-11-29 06:37:31 24 4
gpt4 key购买 nike

我试图使用 @Cookie 获取 cookie 值。但是如果 cookie 值为空,我会收到 400 错误。怎么了?这是我的 Controller :

    public String listContacts(Map<String, Object> map,
HttpServletResponse response, @CookieValue("flag") String flag) {



response.addCookie(new Cookie("flag", "In use"));
...

最佳答案

尝试在 @CookieValue 注释中设置 required = false:

public String listContacts(Map<String, Object> map,
HttpServletResponse response, @CookieValue(value = "flag", required = false) String flag) {

默认情况下,spring 期望 cookie header 存在,否则抛出异常:

Default is true, leading to an exception being thrown in case the header is missing in the request. Switch this to false if you prefer a null in case of the missing header.

Alternatively, provide a defaultValue, which implicitly sets this flag to false.

关于java - Spring MVC 的 Cookie 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18323716/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com