【发布时间】:2018-02-09 03:14:43
【问题描述】:
我是 RESTful API 的菜鸟,我正在尝试构建一个 java 库,该库使用 REST 调用获取身份验证令牌,然后我打算将其存储在 cookie 中,以便我可以将其用于进一步的 API 调用。我已经使用以下代码完成了它:
如何做到这一点?
NewCookie cookie4 = new NewCookie("name", "123");
Response.ok("OK").cookie(cookie4).build();
//call made to retrieve the cookie here, is defined below
我有一个像这样的获取 cookie 方法
@GET
public Response getCookie(@CookieParam("name") Cookie cookie) {
if (cookie == null) {
System.out.println("IN NULL");
return Response.serverError().entity("ERROR").build();
} else {
return Response.ok(cookie.getValue()).build();
}
}
现在我尝试在上面设置 cookie 后检索它:
Cookie cookieval = null;
Response check = getCookie(cookieval);
System.out.println(cookieval.getName());
System.out.println(cookieval.getValue());
它不起作用,流程进入上述getcookie方法中的cookie null部分。我正在使用 JUNIT 测试用例对其进行测试。 cookie 仅与请求对象一起持久化,否则
如果信息不充分或需要更多信息,请告诉我
【问题讨论】:
-
如果是测试用例,请发布完整可运行代码示例。
标签: java rest jax-rs setcookie