我关注了this guide读取和创建 cookie,但我只能读取从同一子域创建的 cookie。
例如,如果我在http://localhost:8080/x/y/test/create
中创建一个cookie,我可以从以下位置读取它:http ://localhost:8080/x/y/test/read
但我无法从http://localhost:8080/x/y/test2/read<读取它
(注意 test 和 test2 之间的区别)
问题出在哪里?我如何才能读取域中各处的 cookie?
这是代码:
1 级
@Path("test")
public class Test {
@GET
@Path("/create")
@Produces(MediaType.TEXT_PLAIN)
public Response login() {
NewCookie cookie = new NewCookie("name", "123");
return Response.ok("OK").cookie(cookie).build();
}
@GET
@Path("/read")
@Produces(MediaType.TEXT_PLAIN)
public Response foo(@CookieParam("name") String value) {
System.out.println(value);
if (value == null) {
return Response.serverError().entity("ERROR").build();
} else {
return Response.ok(value).build();
}
}
}
2 级
@Path("test2")
public class Test2 {
@GET
@Path("/read")
@Produces(MediaType.TEXT_PLAIN)
public Response foo(@CookieParam("name") String value) {
System.out.println(value);
if (value == null) {
return Response.serverError().entity("ERROR").build();
} else {
return Response.ok(value).build();
}
}
}
编辑
问题出现在创建时。现在我以这种方式创建cookie:
NewCookie cookie = new NewCookie("name", "123", "/", "", "comment", 100, false);
最佳答案
这是默认行为。 要将 cookie 设置为您的域,请使用 cookie 的另一个构造函数,并设置空域和根路径:
domain = ""
path = "/"
关于java - 如何读取 JAX-RS( Jersey )中的 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39190230/