Overview
I was working on a problem where any request to a Spring Controller needed to have a Cookie set on its response. Normal page to page navigation worked fine, but I noticed some (not all) of the of AJAX responses were missing the Cookie. Even though I verified through log files and stepping through the code that the Cookie was being set on the Response, once the AJAX Response got back to the browser, there was no Cookie. This article describes how I was able to configure Spring to always return a Cookie on a Spring Controller AJAX Response.CookieInterceptor
A class was created in the project which was responsible for setting the cookie on the Response. Call this class CookieInterceptor and have it implement the org.springframework.web.servlet.HandlerInterceptor interface. A Spring Intecepter is similar to an Java EE Filter in that it allows the request and response to be intercepted and cross cutting concerns be added in the Interceptor instead of adding them to every Controller. In my case, the cross cutting concern was to make sure the Response always had an updated value set for a Cookie. I implemented HandlerInterceptor interface and added the code to set the Cookie in the postHandle() method.Registering the Interceptor with Spring is easy:
<mvc:interceptors>
<bean id="cookieInterceptor" class="com.widgit.CookieInterceptor"/>
</mvc:interceptors>
Testing
After creating CookieInterceptor and registering it with Spring, I performed some testing. Navigating from page to page the Cookie value was found in the Response and the browser was getting the new Cookie as it should....however...
When testing AJAX calls, I got mixed results. If the Controller used the @ResponseBody annotation to return the AJAX Response, I saw no Cookie in the Response. However, if the Controller did NOT use @ResponseBody but instead returned a view string which was turned into a JSON object later, I did get the Cookie. In all cases, I verified through log files and stepping through the code that the code for the CookieInterceptor was being called and the cookie was being set. So why was one case getting the Cookie, and the other case not getting the Cookie?
hey Michael
ReplyDeleteThanks for the information.. Can you please share the example code on how you were setting the cookie using prehandle() method? As I have searched a lot on internet and couldn't find an appropriate solution.. Yours is the closest to what I need to do for my app... thanks! :)
Thank you so much, it worked :)
ReplyDelete