Announcement Announcement Module
No announcement yet.
@CookieValue pass in as parameter Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • @CookieValue pass in as parameter

    I want to pass in some cookie value as parameter using @CookieValue, it works fine when the value exists in cookie. However, if the value doesn't exist in cookie, it will throw an java.lang.IllegalStateException: Missing cookie value, and the whole page crashed.

    What I'm expecting is that it should pass in null so that I can handle it in controller logic. My workaround is by passing in request and loop through all cookie values to find the one I need. Is there any better ways to handle this situation?

  • #2
    One option is checking on the client side (javascript) if the cookie exists before making the request and creating it if it doesn't.


    • #3
      Sorry for bumping up this thread, but I do have a reason; giving an answer that wasn't given.

      If you look at the javadocs of @Cookievalue you would see that you can make the value optional providing a "defaultValue".

          public void showIndex(HttpServletResponse response, HttpServletRequest request, Model model,
                  @CookieValue(value="klasid",defaultValue="") String klasId) {
      Then, you could easily use Stringutils to check for blank.

            if (StringUtils.isNotBlank(klasId)) {
      That way you could prevent nasty javascript or looping through the collection in your request.


      • #4
        Thanks for posting that @froginvasion, this thread is popping high on the results when searching on this topic, very helpful that you posted.