Resteasy. Http client

In this post we’re going to learn how to send Http Requests and receive Http Responses with RESTEasy I recommend using that approach only if you need to make few simple calls and you don’t want to spend your time on creating a “real” client. Otherwise I recommend to use the RESTeasy Proxy Framework that we’ll consider in future posts.

RESTeasy Maven dependencies

First of all we need to add required Maven dependencies for RESTeasy Client


RESTEasy. POST request

Let’s assume we have the RESTful web service (for example BookService from previous posts) that provides CRUD operations.

First we have to create a client

ResteasyClient httpClient = new ResteasyClientBuilder().build();

The url might look like this

String url = "";

Now we want to create a couple of Books, post them to the service and recieve responses from the service.

Book book1 = new Book("The Book 1", "John Doe");
Response response1 =
      .post(Entity.entity(book1, MediaType.APPLICATION_JSON_TYPE));
System.out.println("Added Book 1:\n" + response1.readEntity(String.class));

Book book2 = new Book("The Book 2", "John Doe");
Response response2 =
      .post(Entity.entity(book2, MediaType.APPLICATION_JSON_TYPE));
System.out.println("Added Book 2:\n" + response2.readEntity(String.class));

That gives the console output

Added Book 1: 
{"id":"5ad51915eda57c00041d6e68","title":"The Book 1","author":"John Doe","pages":0}
Added Book 2: 
{"id":"5ad51915eda57c00041d6e69","title":"The Book 2","author":"John Doe","pages":0}

RESTEasy. GET request

Now we want to send a GET request to get a list of books.

Response response =;

Both books are there

[{"id":"5ad51915eda57c00041d6e68","title":"The Book 1","author":"John Doe","pages":0},
{"id":"5ad51915eda57c00041d6e69","title":"The Book 2","author":"John Doe","pages":0}]

RESTEasy. PUT request

Let’s change the second book with a PUT request.

book2.setAuthor("Jane Doe");
response = + "5ad51915eda57c00041d6e69").request()
      .put(Entity.entity(book2, MediaType.APPLICATION_JSON_TYPE));
{"id":"5ad51915eda57c00041d6e69","title":"The Book 2","author":"Jane Doe","pages":0}

We can retrieve the book by its id.

response = + "5ad51915eda57c00041d6e69").request().get();

RESTEasy. DELETE request

We can delete one of books with a DELETE request

response = + "5ad509feeda57c00041d6e64").request().delete();

RESTEasy. Response code

You can easily access to the response code - response.getStatus()

Note: In this examples we get a response body as a String. You can get it as a Book object with Jackson - you need to properly annotate the class Book. Or you can just convert it from String to an object with Gson (see how)

You may also find these posts interesting: