HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Jersey / Set Cookie

Jersey Client – Set Cookie Example

In this example, we will learn to set cookies into HTTP requests invoked by Jersey client. This example makes use of Invocation.Builder for setting cookies into outgoing REST calls.

Set Cookie Example

To set a cookie in REST API request, first get reference of Invocation.Builder from webTarget.request() method, and then use it’s methods.

Client client = ClientBuilder.newClient( new ClientConfig().register( LoggingFilter.class ) );
WebTarget webTarget = client.target("http://localhost:8080/JerseyDemos/rest").path("employees");
 
Invocation.Builder invocationBuilder =  webTarget.request(MediaType.APPLICATION_JSON);
Response response = invocationBuilder
							.cookie("cookieParam1","cookieValue1")
							.cookie(new Cookie("cookieParam2", "cookieValue2"))
							.get();
 
Employees employees = response.readEntity(Employees.class);
//More code

Rest API Code

I have written below REST API for testing purpose.

@GET
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Employees getAllEployees(@CookieParam(value="cookieParam1") String cookieParam1,
								@CookieParam(value="cookieParam2") String cookieParam2) 
{
	System.out.println("cookieParam1 is :: "+ cookieParam1);
	System.out.println("cookieParam2 is :: "+ cookieParam2);
	
	Employees list = new Employees();
	list.setEmployeeList(new ArrayList<Employee>());
	
	list.getEmployeeList().add(new Employee(1, "Lokesh Gupta"));
	list.getEmployeeList().add(new Employee(2, "Alex Kolenchiskey"));
	list.getEmployeeList().add(new Employee(3, "David Kameron"));
	
	return list;
}

Demo

Now let’s call above REST API using Jersey client code as suggested in first heading.

public static void main(String[] args) 
{
	Client client = ClientBuilder.newClient( new ClientConfig().register( LoggingFilter.class ) );
	WebTarget webTarget = client.target("http://localhost:8080/JerseyDemos/rest").path("employees");
	 
	Invocation.Builder invocationBuilder =  webTarget.request(MediaType.APPLICATION_JSON);
	Response response = invocationBuilder
								.cookie("cookieParam1","cookieValue1")
								.cookie(new Cookie("cookieParam2", "cookieValue2"))
								.get();
	 
	Employees employees = response.readEntity(Employees.class);
	List<Employee> listOfEmployees = employees.getEmployeeList();
	     
	System.out.println(response.getStatus());
	System.out.println(Arrays.toString( listOfEmployees.toArray(new Employee[listOfEmployees.size()]) ));
}
Output: 

On client side:

Oct 01, 2015 4:53:59 PM org.glassfish.jersey.filter.LoggingFilter log
INFO: 1 * Sending client request on thread main
1 > GET http://localhost:8080/JerseyDemos/rest/employees
1 > Accept: application/json
1 > Cookie: $Version=1;cookieParam1=cookieValue1,$Version=1;cookieParam2=cookieValue2

On server side:

cookieParam1 is :: cookieValue1
cookieParam2 is :: cookieValue2

Happy Learning !!

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.

Share this:

  • Twitter
  • Facebook
  • LinkedIn
  • Reddit

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. Abhishek Patel

    January 31, 2016

    Good Example !!

Comments are closed on this article!

Search Tutorials

Jersey Tutorial

  • Jersey – Hello World
  • Jersey2 – Hello World
  • Jersey – quickstart-archetype
  • Jersey – Custom Logging
  • Jersey – Set Cookie
  • Jersey – File Download
  • Jersey – File Upload
  • Jersey – Multi-File Upload
  • Jersey – Exception Handling
  • Jersey – MOXy JSON
  • Jersey – JSONP
  • Jersey – Google Gson
  • Jersey – Security

Jersey Client

  • Jersey Client – Access REST APIs
  • Jersey Client – Authentication
  • Jersey Client – Set Cookie

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Java 15 New Features
  • Sealed Classes and Interfaces
  • EdDSA (Ed25519 / Ed448)