What you do in your example has nothing to do with cookies. session.setAttribute ("key", valueObject); Specifies a java object in a session. The session is stored on the server. Sessionid is the only thing passed to the client. It can be a cookie or it can be in a URL. Attributes in a session are not serialized for strings.
Cookies, on the other hand, are strings that are sent back to the client. The client is required to store their cookies (and some disable them) and return them to the server.
Setting a cookie value from a complex graphic object will require serialization and deserialization. The session attribute will not be.
If you want to read a cookie, use this:
@CookieValue("key") String cookie
In the list of controller parameters. The cookie variable will be populated with the value from the cookie named "key".
To set a cookie, call:
response.addCookie(cookie);
source share