AbacusUtil Docs | Download | Javadoc | FAQ | Community

Http Client

By the methods provided in HttpClient, it's super easy to send(get/post/put/delete...) String/Map/Entity/InputStream/Reader... request with JSON/XML... format. Here is a sample:

/*
To run the test, you need to configure servlet: EchoServlet in web.xml under tomcat and update the url accordingly.
The EchoServlet just sends back what the server receives from client.

<servlet>
     <servlet-name>echo</servlet-name>
     <servlet-class>com.landawn.abacus.http.EchoServlet</servlet-class>
</servlet>        
...
<servlet-mapping>
     <servlet-name>echo</servlet-name>
     <url-pattern>/echo</url-pattern>
</servlet-mapping>
*/
@Test
public void test_HttpClient() {
    final String url = "http://localhost:8080/abacus/echo";
    final HttpClient httpClient = HttpClient.create(url);

    Account account = createAccount();
    String request = "<get><entityId entityName=\"Account\"><id>" + account.getId() + "</id></entityId></get>";

    String response = httpClient.get(N.asMap("call", request));
    N.println(response);
    // Response: call=<get><entityId entityName="Account"><id>0</id></entityId></get>

    response = httpClient.get(account);
    N.println(response);
    // Response: id=0&gui=c3f39f3b-7ebb-4575-a987-0b3a7ecb5ab5&firstName=Jack&lastName=Ye&status=0&lastUpdateTime=2015-08-04T00:24:45.411Z&createTime=2015-08-04T00:24:45.411Z

    response = httpClient.post(request);
    N.println(response);
    // Response: <get><entityId entityName="Account"><id>0</id></entityId></get>
    assertEquals(request, response);

    // send entity object with JSON format
    Map<String, Object> settings = N.asMap(HTTP.CONTENT_FORMAT, ContentFormat.JSON);
    response = httpClient.post(account, settings);
    N.println(response);
    // Response: {"id":0, "gui":"8528c81f-6e98-4cea-aeeb-e60dcfadc9bc", "firstName":"Jack", "lastName":"Ye", "status":0, "lastUpdateTime":1438647775174, "createTime":1438647775174}
    assertEquals(N.toJSON(account), response);

    // Specific response object is composed by JSON response from server.
    Account account2 = httpClient.post(Account.class, account, settings);
    assertEquals(account, account2);

    // send entity object with JSON format and compressed by LZ4
    settings = N.asMap(HTTP.CONTENT_FORMAT, ContentFormat.JSON_LZ4);
    response = httpClient.post(account, settings);
    N.println(response);
    // Response: {"id":0, "gui":"8528c81f-6e98-4cea-aeeb-e60dcfadc9bc", "firstName":"Jack", "lastName":"Ye", "status":0, "lastUpdateTime":1438647775174, "createTime":1438647775174}
    assertEquals(N.toJSON(account), response);

    // send entity object with XML format
    settings = N.asMap(HTTP.CONTENT_FORMAT, ContentFormat.XML);
    response = httpClient.post(account, settings);
    N.println(response);
    // Response: <account><id>0</id><gui>8528c81f-6e98-4cea-aeeb-e60dcfadc9bc</gui><firstName>Jack</firstName><lastName>Ye</lastName><status>0</status><lastUpdateTime>1438647775174</lastUpdateTime><createTime>1438647775174</createTime></account>
    assertEquals(N.toXML(account), response);

    // send entity object with XML format and compressed by Snappy
    settings = N.asMap(HTTP.CONTENT_FORMAT, ContentFormat.XML_SNAPPY);
    response = httpClient.post(account, settings);
    N.println(response);
    // Response: <account><id>0</id><gui>8528c81f-6e98-4cea-aeeb-e60dcfadc9bc</gui><firstName>Jack</firstName><lastName>Ye</lastName><status>0</status><lastUpdateTime>1438647775174</lastUpdateTime><createTime>1438647775174</createTime></account>
    assertEquals(N.toXML(account), response);
}