2 min read

RESTEasy client to send JSON requests

RESTEasy client to send JSON requests

This is a very short example of how you can use RESTEasy to send JSON requests. For one of my pet projects, I needed to send requests to my JSON based API. I usually use Apache‘s HttpClient for simple requests. But, this time I wanted to try some of JAX-RS implementations. As for everything else in Java, the selection of implementations is pretty wide with Apache CXF, Restlet, Jersey, and RESTEasy being one of them. RESTEasy is JBoss’ implementation of both JAX-RS 2.0 and JAX-RS 2.1.

RESTEasy logo

Setting up RESTEasy

Most of the examples you can find are of how to send XML data. For sending JSON you will need the following dependencies:



The Jackson 2 provider will be automatically loaded by RESTEasy. There is also a way to load providers manually but in this case, you don’t need to do that.

The model for my example is a simple User class. This POJO will be converted to JSON when sending requests, and the other way around when receiving responses.

public class User {
    private Long id;
    private String name;
    private String surname;
    private String username;

The annotations are from the Lombok and are not related to RESTEasy. They automatically add boilerplate code like like getters, setters, and no-arg constructors among other things.

RESTEasy has a client proxy framework that lets you use JAX-RS annotations to define all the HTTP requests you want to send. All you need to do is to write a Java interface and use the correct JAX-RS annotations on methods and the interface.

public interface UserClient {

    User getUserById(@QueryParam("id") String id);

    User createUser(User user);

Sending the request

You first need to build the client and create the proxy based on an interface like the one we just created. All you need to do then is invoke the methods of the proxy. The invoked method gets translated to an HTTP request based on how you annotated the method and get sent to the server. In hindsight, RESTEasy uses Apache’s HTTP client but it does all the manual labor for you.

ResteasyClient client = new ResteasyClientBuilder().build();
ResteasyWebTarget webTarget = client.target(UriBuilder.fromPath(ENDPOINT));
UserClient userClient = webTarget.proxy(UserClient.class);

User user = userClient.getUserById("1");

User nextUser = new User();