Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Consuming Web APIs Using HTTP-RPC 5.5

DZone's Guide to

Consuming Web APIs Using HTTP-RPC 5.5

Let's take a look at consuming web APIs using the new version of HTTP-RPC 5.5. Also look at different methods of how service operations are invoked.

· Integration Zone ·
Free Resource

How to Transform Your Business in the Digital Age: Learn how organizations are re-architecting their integration strategy with data-driven app integration for true digital transformation.

HTTP-RPC is an open-source framework for implementing RESTful and REST-like services in Java. It is extremely lightweight and requires only a Java runtime environment and a servlet container. The entire framework is distributed as a single JAR file that is about 52KB in size, making it an ideal choice for applications such as microservices where a minimal footprint is desired.

HTTP-RPC version 5.5 is now available for download and via Maven Central. The primary enhancement in this release is the new WebServiceProxy class, which allows an HTTP-RPC web service to act as a consumer of other REST services.

Service proxies are initialized via a constructor that takes the following arguments:

  • method — the HTTP method to execute
  • url — an instance of java.net.URL representing the target of the operation

Request headers and arguments are specified via the proxy's getHeaders() and getArguments() methods, respectively. Like HTML forms, arguments are submitted either via the query string or in the request body. As with HTML, POST requests may be submitted as either "application/x-www-form-urlencoded" or "multipart/form-data" (specified via the proxy's setEncoding() method). Custom request encodings are also supported.

Service operations are invoked via one of the following methods:

public <T> T invoke() throws IOException { ... }
public <T> T invoke(ResponseHandler<T> responseHandler) throws IOException { ... }

The first version automatically deserializes a successful response using HTTP-RPC's JSONDecoder class. The second version allows a caller to perform custom deserialization of the server response.

For example, the following code snippet demonstrates how WebServiceProxy might be used to access the operations of a simple math service:

// GET /math/sum?a=2&b=4
WebServiceProxy webServiceProxy = new WebServiceProxy("GET", new URL("http://localhost:8080/httprpc-test/math/sum"));

webServiceProxy.getArguments().put("a", 4);
webServiceProxy.getArguments().put("b", 2);

Number result = webServiceProxy.invoke();

System.out.println(result); // 6.0

// GET /math/sum?values=1&values=2&values=3
WebServiceProxy webServiceProxy = new WebServiceProxy("GET", new URL("http://localhost:8080/httprpc-test/math/sum"));

webServiceProxy.getArguments().put("values", Arrays.asList(1, 2, 3));

Number result = webServiceProxy.invoke();

System.out.println(result); // 6.0

Typed Web Service Access

Additionally, the adapt() methods of the WebServiceProxy class can be used to facilitate type-safe access to web services:

public static <T> T adapt(URL baseURL, Class<T> type) { ... }
public static <T> T adapt(URL baseURL, Class<T> type, Map<String, ?> headers) { ... }

Both versions take a base URL and an interface type as arguments and return an instance of the given type that can be used to invoke service operations. The second version also accepts a map of header values that will be submitted with every service request.

The RequestMethod annotation is used to associate an HTTP verb with an interface method. The optional ResourcePath annotation can be used to associate the method with a specific path relative to the base URL. If unspecified, the method is associated with the base URL itself.

For example, the following interface might be used to model the operations of the example math service:

public interface MathService {
    @RequestMethod("GET")
    @ResourcePath("sum")
    public Number getSum(double a, double b) throws IOException;

    @RequestMethod("GET")
    @ResourcePath("sum")
    public Number getSum(List<Double> values) throws IOException;
}

This code snippet uses the adapt() method to create an instance of MathService, then invokes the getSum() methods on the returned instance. The results are identical to the previous example:

MathService mathService = WebServiceProxy.adapt(new URL("http://localhost:8080/httprpc-test/math/"), MathService.class);

// GET /math/sum?a=2&b=4
mathService.getSum(4, 2); // 6.0

// GET /math/sum?values=1&values=2&values=3
mathService.getSum(Arrays.asList(1.0, 2.0, 3.0)); // 6.0

Summary

This article introduced HTTP-RPC's new WebServiceProxy class, which allows an HTTP-RPC service to act as a consumer of other REST services. For more information, see the project README.

Make your mark on the industry’s leading annual report. Fill out the State of API Integration 2019 Survey and receive $25 to the Cloud Elements store.

Topics:
java ,open source ,rest ,release ,integration ,http-rpc ,apis ,web apis

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}