Categories
Java

Rest Client For Java using HttpClient and Jakson

There are many HttpClient and Rest Clients lib is available for Java developers. The most common is Spring Rest Template for REST client and Apache HttpClient a generic HTTP client.

I personally like the Apache HttpClient library for making REST calls and Http Calls.

I found one generic kind of REST client using Apache HttpClient and Jackson ObjectMapper library. I used ObjectMapper for JSON serialization and de-serialization. You can check the Git repository for the same here http-rest-client

I think this can be good to start making or using a REST client for java developers.

This library provides overloaded methods to make GET, POST, PUT, PATCH, and DELETE http requests with options to send JSON body, custom headers, and URI parameters (Query String).

Method to make calls


public <T> T executeGet(final String url, final Class<T> classType)
public <T> T executeGet(final String url, final Map<String, String> params, final Class<T> classType)
public <T> T executeGet(final String url, final Map<String, String> headers, final Map<String, String> params,
final Class<T> classType)

public <T> T executeDelete(final String url, final Class<T> classType)
public <T> T executeDelete(final String url, final Map<String, String> params, final Class<T> classType)
public <T> T executeDelete(final String url, final Map<String, String> headers, final Map<String, String> params,
final Class<T> classType)

public <T> T executePost(final String url, final Object payLoad, final Class<T> classType)
public <T> T executePost(final String url, final Object payLoad, final Map<String, String> params,
final Class<T> classType)
public <T> T executePost(final String url, @Nullable final Object payLoad,
@Nullable final Map<String, String> headers, @Nullable final Map<String, String> urlparams,
final Class<T> classType)

public <T> T executePut(final String url, final Object payLoad, final Class<T> classType)
public <T> T executePut(final String url, final Object payLoad, final Map<String, String> params,
final Class<T> classType)
public <T> T executePut(final String url, @Nullable final Object payLoad,
@Nullable final Map<String, String> headers, @Nullable final Map<String, String> urlparams,
final Class<T> classType)

public <T> T executePatch(final String url, final Object payLoad, final Class<T> classType)
public <T> T executePatch(final String url, final Object payLoad, final Map<String, String> params,
final Class<T> classType)
public <T> T executePatch(final String url, @Nullable final Object payLoad,
@Nullable final Map<String, String> headers, @Nullable final Map<String, String> urlparams,
final Class<T> classType)

Some example to use this Rest client library

Make Post request example


final HttpRestClient client = HttpRestClient.createDefault();
final Map<String, String> payLoad = new HashMap<String, String>();
payLoad.put("username", "admin");
payLoad.put("password", "admin");
final Map<String, String> strMap = new HashMap<String, String>();
final Map<String, String> data = client.executePost("http://127.0.0.1:1080/login", payLoad, strMap.getClass());
assertEquals(TOKEN, data.get("token"));

Mager Get Request example


final HttpRestClient client = HttpRestClient.createDefault();
final Map<String, String> strMap = new HashMap<String, String>();
strMap.put("param1", "10");

final Map<String, String> strHeader = new HashMap<String, String>();
strHeader.put("header1", "10");

final Map<String, String> data = client.executeGet("http://127.0.0.1:1080/param", strHeader, strMap,
strMap.getClass());
assertEquals("10", data.get("param1"));

Make Delete request example


final HttpRestClient client = HttpRestClient.createDefault();
final Map<String, String> strMap = new HashMap<String, String>();
strMap.put("param1", "10");

final Map<String, String> strHeader = new HashMap<String, String>();
strHeader.put("header1", "10");

final Map<String, String> data = client.executeDelete("http://127.0.0.1:1080/param", strHeader, strMap,
strMap.getClass());
assertEquals("10", data.get("param1"));

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.