java spark framework can not set or read cookie

1.8k views Asked by At

I am new to the spark framework. I got problem when i was trying to set cookie to response or read cookie from request.

The route:

post("/test/set/cookie", TestController.setCookie);
        get("/test/get/cookie", TestController.getCookie);
        post("/test/remove/cookie", TestController.removeCookie);

controller:

public static Route setCookie = (Request request, Response response) -> {
        System.out.println("set");
        String id = UUID.randomUUID().toString();
        response.cookie("test1", id, 3600, false, true);
        return "done";
    };

    public static Route getCookie = (Request request, Response response) -> {
        System.out.println("get");
        String cookie = request.cookie("test1");
        System.out.println(cookie);
        return "done";
    };

    public static Route removeCookie = (Request request, Response response) -> {
        System.out.println("remove");
        response.removeCookie("test1");
        return "done";
    };

Postman set cookie

Postman get cookie faile

what i am missing?

1

There are 1 answers

0
Zerg.to_terrain On

I solved it finally---

response.cookie("/", "test", "value", 3600, false, true);

just need to add the path "/" to the cookie.