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
The url might look like this
Now we want to create a couple of Books, post them to the service and recieve responses from the service.
That gives the console output
RESTEasy. GET request
Now we want to send a GET request to get a list of books.
Both books are there
RESTEasy. PUT request
Let’s change the second book with a PUT request.
We can retrieve the book by its id.
RESTEasy. DELETE request
We can delete one of books with a DELETE request
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)