【问题标题】:How to read a cookie in JAX-RS (Jersey)如何在 JAX-RS (Jersey) 中读取 cookie
【发布时间】:2017-01-04 12:56:21
【问题描述】:

我跟随 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 读取它(注意区别在 testtest2)

之间

问题出在哪里?我怎么能在我的域中到处读取 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);

【问题讨论】:

    标签: java rest cookies jax-rs


    【解决方案1】:

    这是一种默认行为。 要将 cookie 设置为您的域,请为 cookie 使用另一个构造函数,并设置空域和根路径:

    domain = ""
    path = "/"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-24
      • 2016-12-02
      • 1970-01-01
      • 2018-12-16
      • 2015-06-16
      • 1970-01-01
      • 2017-07-02
      相关资源
      最近更新 更多