如何在java库代码中使用JAX-RS设置和检查cookie



我是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只对请求对象持久,而不是

如果信息不足或需要更多信息,请告诉我

我看到了您的代码,看起来问题是第一行将cookieval声明为"null",然后将null值传递给方法getCookie,后者将其识别为null。

Cookie cookieval = null;
Response check = getCookie(cookieval);
System.out.println(cookieval.getName());
System.out.println(cookieval.getValue());

最新更新