2017-08-31 44 views
0

我是一个使用RESTful API的noobie,我正在尝试构建一个java库,它使用REST调用获取身份验证令牌,然后我打算将其存储在一个cookie中,以便我可以将其用于进一步的API调用。我用下面的代码完成了它:如何使用java库代码设置和检查JAX-RS的cookie

这怎么能实现?

NewCookie cookie4 = new NewCookie("name", "123"); 
Response.ok("OK").cookie(cookie4).build(); 
//call made to retrieve the cookie here, is defined below 

我有一个像这样

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

一个get cookie的方法,现在我尝试检索饼干后,我把它上面:

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

它不工作时,进入到上面的getcookie方法的cookie null部分。我正在使用JUNIT测试用例来测试它。该cookie是只有一个请求对象持久化没有其他

请让我知道,如果信息不充分或需要更多的信息

+0

如果是测试用例,请发布_complete_可运行代码示例。 –

回答

0

我看到你的代码,看起来问题出在哪里声明cookieval作为一线“null”,然后将空值传递给getCookie方法,该方法将其识别为null。

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