Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/rest/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在JAX-RS(Jersey)中读取cookie_Java_Rest_Cookies_Jax Rs - Fatal编程技术网

Java 如何在JAX-RS(Jersey)中读取cookie

Java 如何在JAX-RS(Jersey)中读取cookie,java,rest,cookies,jax-rs,Java,Rest,Cookies,Jax Rs,我随后读取并创建cookie,但我只能读取从同一子域创建的cookie 例如,如果我在http://localhost:8080/x/y/test/createI可以从以下位置读取它:http://localhost:8080/x/y/test/read但我无法从http://localhost:8080/x/y/test2/read(注意测试和测试2之间的区别) 问题在哪里?我怎么能在我的领域里到处阅读cookie 代码如下: 第一类 @Path("test") public class Te

我随后读取并创建cookie,但我只能读取从同一子域创建的cookie

例如,如果我在
http://localhost:8080/x/y/test/create
I可以从以下位置读取它:
http://localhost:8080/x/y/test/read
但我无法从
http://localhost:8080/x/y/test2/read
(注意测试和测试2之间的区别)

问题在哪里?我怎么能在我的领域里到处阅读cookie

代码如下:

第一类

@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();
        }
    }

}
第二类

@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 = "/"