未设置Set-Cookie标头

时间:2016-11-17 11:40:50

标签: java servlets cookies jersey response

我想通过Set-Cookie标头从我的服务器收到回复。我在服务器端使用Jersey,spring-boot和Spring安全,在客户端使用angular 2。我收到响应,但未设置Set-Cookie标头。它只在元数据中出现:

  

{" statusType":" OK""实体":空,"的EntityType":空,"元数据&# 34;:{"的Set-Cookie":[{"名称":" SomeName""值":" someId&# 34;,"版本":1,"路径":空,"结构域":空,"评价":空,&#34 ; MAXAGE": - 1,"安全":假}]},"状态":200}

但是没有" Set-Cookie"标题: Header

来自回复的代码:

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

方法" .header(..)"也没有工作。

2 个答案:

答案 0 :(得分:2)

尝试这样

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

Snippet from Chrome DevTool

ResponseEntityHttpServletResponseCookie是从javax.servlet.http.*

导入的

答案 1 :(得分:2)

另一种选择:

public ResponseEntity<?> resourceFunction() {
    HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.add(HttpHeaders.SET_COOKIE, "SomeName=someId");
    return new ResponseEntity(httpHeaders, HttpStatus.OK);
}