I have an existing controller method
@RequestMapping(value = "/{productId}", method = RequestMethod.GET)
public ModelAndView getProduct(@PathVariable String productId){
// code in this method does not matter
}
I need to enhance the existing behavior of this route by analyzing user's cookie. If user has the cookie named xyz I need to make a different stuff. So I created an overloaded version of getProduct method by adding the second parameter @CookieValue("xyz") final String xyz:
@RequestMapping(value = "/{productId}", method = RequestMethod.GET)
public ModelAndView getProduct(@PathVariable String productId, @CookieValue("xyz") final String xyz){
// make some different logic
}
When I try to access this route, spring always throw an exception
org.springframework.web.bind.ServletRequestBindingException: Missing cookie named 'xyz' for method parameter type [java.lang.String]
Obviously the overloaded version of the method getProduct is always called.
How can I fix this bug? Is there any annotation to tell that the cookie value is optional?
Just found that there is required attribute for @CookieValue annotation
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With