Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring mvc controller - missing cookie named 'xyz'

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?

like image 504
Yev Avatar asked Nov 22 '15 16:11

Yev


1 Answers

Just found that there is required attribute for @CookieValue annotation

like image 108
Yev Avatar answered Dec 20 '22 07:12

Yev