M. Thiele M. Thiele - 18 days ago 9
Java Question

Set-Cookie header is not set

I want to receive a response from my server with a Set-Cookie header. I'm using Jersey, spring-boot and Spring security on the server side and angular 2 on the client side. I receive the response, but the Set-Cookie header is not set. It's only in the metadata:


{"statusType":"OK","entity":null,"entityType":null,"metadata":{"Set-Cookie":[{"name":"SomeName","value":"someId","version":1,"path":null,"domain":null,"comment":null,"maxAge":-1,"secure":false}]},"status":200}


But there is no "Set-Cookie" Header:
Header

That's the code from the response:

return Response.ok().cookie(new NewCookie("SomeName", "someId")).build();


The method ".header(..)" isn't working as well.

Answer

Try it like this

public ResponseEntity<?> resourceFunction(HttpServletResponse response) {
    response.addCookie(new Cookie("SomeName", "someId"));
    return ResponseEntity.ok().build();
}

Snippet from Chrome DevTool

ResponseEntity, HttpServletResponse and Cookie are imported from javax.servlet.http.*

Comments