Yev*_*Yev 5 mapping cookies spring-mvc optional-parameters spring-3
我有一个现有的控制器方法
@RequestMapping(value = "/{productId}", method = RequestMethod.GET)
public ModelAndView getProduct(@PathVariable String productId){
// code in this method does not matter
}
Run Code Online (Sandbox Code Playgroud)
我需要通过分析用户的 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
}
Run Code Online (Sandbox Code Playgroud)
当我尝试访问此路由时,spring 总是抛出异常
org.springframework.web.bind.ServletRequestBindingException: Missing cookie named 'xyz' for method parameter type [java.lang.String]
Run Code Online (Sandbox Code Playgroud)
显然,方法 getProduct 的重载版本总是被调用。
我该如何修复这个错误?是否有任何注释表明 cookie 值是可选的?
| 归档时间: |
|
| 查看次数: |
2641 次 |
| 最近记录: |