【问题标题】:spring mvc controller - missing cookie named 'xyz'spring mvc 控制器 - 缺少名为“xyz”的 cookie
【发布时间】:2015-11-22 16:03:21
【问题描述】:

我有一个现有的控制器方法

@RequestMapping(value = "/{productId}", method = RequestMethod.GET)
public ModelAndView getProduct(@PathVariable String productId){
 // code in this method does not matter
}

我需要通过分析用户的 cookie 来增强这条路由的现有行为。如果用户有名为 xyz 的 cookie,我需要制作不同的东西。 所以我通过添加第二个参数 @CookieValue("xyz") final String xyz 创建了一个重载版本的 getProduct 方法:

@RequestMapping(value = "/{productId}", method = RequestMethod.GET)
public ModelAndView getProduct(@PathVariable String productId, @CookieValue("xyz") final String xyz){
 // make some different logic 
}

当我尝试访问这条路由时,spring总是抛出异常

org.springframework.web.bind.ServletRequestBindingException: Missing cookie named 'xyz' for method parameter type [java.lang.String]

显然,getProduct 方法的重载版本总是被调用。

我该如何修复这个错误?是否有任何注释告诉cookie值是可选的?

【问题讨论】:

    标签: spring-mvc cookies mapping spring-3 optional-parameters


    【解决方案1】:

    刚刚发现@CookieValue注解有required属性

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-03
      • 2011-08-07
      • 1970-01-01
      • 2016-07-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-13
      • 1970-01-01
      相关资源
      最近更新 更多