-1

How to Maintain state Jersey Client

    { //
      //      Some session logic 
      //
     } 
        Client client = ClientBuilder.newClient();
        WebTarget baseTarget =
        client.target("https");
        /
        MultivaluedMap<String, String> formData = new 
        MultivaluedHashMap<String, String>();
        formData.add("usr", "@gmail.com");
        formData.add("pwd", "mat");
        Response response = baseTarget.request().post(Entity.form(formData))  
        System.out.println("----Second-time--method invoked GET-------");

        Response resp_sec = base2Target.request().get(); //second time in session perform action client side

See if this Helps.This code provides me a template.hope it works for you.

1 Answers1

0

You first need to get the cookies from the initial response. These will be NewCookie instances, which are the cookies that the server sends to the client, as Set-Cookie headers. On the client side, you need to send a Cookie back to the server, which will result in a Cookie header being sent. If you try to set a NewCookie on the client, it would set a Set-Cookie header, which would be wrong. You can easily convert a NewCookie to a Cookie simply by calling newCookie.toCookie()

Map<String, NewCookie> cookies = response.getCookies();
Invocation.Builder ib = baseTarget.request();
for (NewCookie cookie: cookies.values()) {
    ib.cookie(cookie.toCookie());
}
Response response = ib.get();

See also:

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • How to Convert Java JERSEY Response object (which contains JSON ) to JAVA Class https://stackoverflow.com/questions/46894972/how-to-convert-java-jersey-response-object-which-contains-json-to-java-class – shoaib shaikh Oct 23 '17 at 17:16