WebAPIs provide a way to expose functionality to the web in a generic way that any language with http support should be able to consume the API created. In one such project I recently worked on, I was able to test this out. This time using a Java (in my case console) to call the .NET WebAPI. Not the Java application is console since really all applications are consoles and I didn’t need to create a fancy UI or demonstrate anything other than calling the WebAPI. On the .NET side, it is .NET framework. I was doing work for an industry that is a bit behind and not doing anything with dotnet core.
This first step of the process will be to create a ASP.NET WebAPI project so that we have the API up and working, even if just a simple shell for initial testing. Below is a link to the tutorial you can use.
Using this example we only need to do the first step, Create a web project as this will give us a sample WebAPI that will return 2 values on the get method, like so:
[“value1″,”value2”]
The WebAPI method itself looks like:
public IEnumerable<string> Get()
{
// return newly created array
return new string[] { “value1”, “value2” };
}
and to test you can browse to the method using http://localhost:port/api/values once you launch the project in visual studio or another editor. Note that “port” needs to be replaced with the port the project is running on.
So we have an API setup, let’s call it. To start this process we need to create a Java application. To do this there are countless tutorials online. I have provided a link to just one of them. This tutorial will provide you a console application writing a single line to the console, sufficient for our needs.
https://www.jetbrains.com/help/idea/creating-and-running-your-first-java-application.html
In Java it is helpful to know how to add references to a project. Below is another link with a section on Module dependencies. In this post we will need the following modules:
https://www.jetbrains.com/help/idea/creating-and-managing-modules.html
Modules:
– org.apache.httpcomponents:httpclient
– org.apache.httpcomponents:httpcore
With the project setup and the modules added we just need to add a method to call our API. First we can setup the basics of the request including the URL, and headers:
String url = “http://localhost:port/api/values/;
HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(url);
// add request header
request.addHeader(“User-Agent”, “Mozilla/5.0”);
Then we can execute the request and return the response object for our call. Here we can also print the status code returned in the response to verify the request was successful (200s – https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml).
HttpResponse response = client.execute(request);
System.out.println(“Response Code : ” + response.getStatusLine().getStatusCode());
Once we have the response we can load it into a BufferReader and read the results line by line printing the returned values at the very end. With a number of ways to do this, I have chosen a while look as demonstrated below.
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuilder result = new StringBuilder();
String line = “”;
while ((line = rd.readLine()) != null) {
result.append(line);
}
System.out.println(result.toString());
This should return the same values as when we browsed to the API while we were creating it above.
[“value1″,”value2”]
Now we have a Java application calling into a ASP.NET WebAPI. This is helpful as the dotnet core code for the WebAPI isn’t that much different if we wish to update that portion of the project. In my case, I needed to stick with .NET Framework as the customer is an enterprise customer who has not adopted more modern frameworks like dotnet core.
Another thing you might be thinking is why is this relevant? Why not write the whole solution in .NET or Java, well in my case the customer I was working with was a Java shop and the product they wanted to interface with was mainly .NET. While there were libraries in Java, they knew the .NET libraries were the focus of the company that created the software. Unfortunately, that software did not have a standard WebAPI to hook into, instead an object model was exposed. Thus, I wrote a WebAPI to wrap the object model in a standard WebAPI they could call from pretty much any language. WebAPI uses .NET to tightly couple the software package’s API, exposes WebAPI to everyone else. Now they can code in Java to complete their side of the project with no issue.