Hi everyone.
Welcome back.
In this lecture we will go and learn about the new HTTP client.
That's part of modern Java.
I'll start with some theory first and then we'll get to the coding part.
The new HTTP client API got released in Java 11.
The nicest thing about this particular HTTP client is that it has a support for HTTP two and websockets
on top of it.
This client also has the ability to build HTTP clients in synchronous and asynchronous mode.
If you have questions on what does it mean, don't worry about it.
We will be coding both of these types in action.
That's all about the theory part.
With this information, let's jump right in and start coding this HTTP two client.
So I'm back in IntelliJ over here.
So we have this J web server still up and running.
If I go to the browser and then try to hit this endpoint like if I go to movies dot JSON, it still
gives me this response.
And then if I go to the movies by id JSON, this still responds with a valid movie JSON.
So to test our HTTP client, we need to have this J web server up and running.
So I'm back in IntelliJ here.
So we are going to be working on the HTTP client.
Right.
So you will notice the HTTP package under the explorer jar latest Java features project.
So let's go and open that.
And then you will see a representation of our domain class named movie.
So this is a representation of all the properties we have seen in the JSON response.
So if you go to the browser again so the browser has or the JSON has the movie ID name, let's zoom
in a little bit.
So we have the movie ID name here cast and release date.
So this is exactly what we have in this record class.
So what we are going to do is we are going to be making a call to that service, and then we are going
to be parsing the response and map it to this object.
Okay.
So let's go back to the HTTP client.
In this case we have a class named movies client.
So we are going to be working on building functions that's going to interact with these two services.
So in order to save some time what I did was I have created these two constants.
So the first one actually the URL that gives you all the ten movies, that is part of this JSON and
this is the one which is movie id by URL.
This actually calls this movie by id JSON, which actually returns just one single object.
Okay.
So now with this information, what we will do is as part of this lecture, we are going to build a
HTTP client that's going to be interacting with this particular URL.
So the very first thing here is that we are going to create a function which is going to be public.
And this function is going to be returning a movie object.
Okay.
And then the movie function name is going to be get movie by ID.
Okay.
This function is not going to get any method parameters.
It's going to be a simple function.
But this function is going to interact with this particular URL.
So what do we need here right now.
So we need the client.
And then we need the request object using which we can actually pass the URL.
So very first thing is how do we build a client.
So we can actually build a client as a class variable.
So in this case we can call this one as HTTP client.
So HTTP client is part of the Java dot.
Net dot http package.
And then let's name this one as client okay.
And this client has a builder pattern using which we can build a HTTP client.
So in this case so this is the builder right.
And then let's set up a connection timeout just to show you what are the different options that we have.
So in this case we press Dot here.
It gives you all these different options we have.
But in here I'm going to keep this example really simple.
What I'm going to do I'm going to set up a connection timeout.
So in this case it's going to be duration of seconds.
And the number of seconds is going to be two seconds.
So I'm setting up a connection timeout value to be two seconds.
So using the client what can we do.
Let's use this client over here and then press Dot over here.
It's going to give you different options.
So in this case you notice this send and send async.
So send is basically the synchronous way of building the HTTP client.
What does this mean is that when you make this call the calling thread will be blocked until the response
is received from the service.
And then we continue with the code execution.
Send async.
As you can see, the response is going to be of type completablefuture.
So that's the asynchronous way of building a HTTP client.
So we're going to start with a synchronous approach because that is very simple and straightforward.
So we're going to be making a call to this one.
And if you go to the function what it accepts is a HTTP request.
So what we are going to build is we are going to build the HTTP request instance.
And then we need to provide a body handler also.
Okay.
So now I'm going to create a function because we need a HTTP request for different URLs.
So I'm going to create a generic function so that we can use it for both the movies by id URL and all
movies URL.
So I'm going to create a static function.
It's going to be public static HTTP request.
So that's a response or return type of this particular function.
And then I'm going to give a name of this function as request builder okay.
And this function is going to accept a URL.
So in this case string URL.
There you go.
And then we are going to be calling the HTTP request.
So this also have a builder pattern implemented.
So HTTP request dot new builder.
And then you can actually pass the function or the type of HTTP call you want to make.
So in this case it's going to be the get call and then dot build okay.
But this is not enough right.
We need to provide which URL we are going to be interacting with.
So in this case it's going to be URI.
So you can call URI create.
And then we can provide the URL over here.
So this is going to create a HTTP request for any kind of URL that you are going to be calling this
function with.
So in this case we can actually call request builder function and then pass the URL as movie by id url.
So in this case this is going to give you a response as a request.
So this is going to be request over here.
There you go.
And then we are going to be passing that to this one okay.
And after that what is the response body handler.
So if you take a look at HTTP response and then we can actually call dot body handlers.
And then you can actually call off string.
So I'm going to pass the response as a string.
And then we can use some serialization and deserialization library to actually convert that to a movie
object.
Okay, there you go.
And what this is going to return you.
So this is going to return your HTTP response.
So I'm going to name this one as where response equal to this one.
And then I believe we need to add some kind of exception handler over here.
There you go I'm going to have a generic exception block so that we don't have to deal with specific
exceptions at the moment okay.
And then I'm going to add an error to print the actual exception.
But in reality if you are using a logging library you can use a logger.
There are many options available to handle this one.
Now the next option is I want to parse this response object, which is actually returning the response
body as a string.
Right.
We want to convert that to a movie object.
So how can we achieve that?
There are many deserialization and serialization libraries available, but for this specific course
I'm planning to use the Jackson mapper.
So if you go to the build.gradle file over here you will notice this Jackson libraries.
So this Jackson libraries has some API's or library code using which we can convert a string JSON into
a Java object or a Java object to a string JSON.
So in our case we want to convert a string JSON to a Java object.
So I'm going to create an instance of object mapper so that we can use it across this specific class.
So in here let's go ahead and create an object mapper instance private final.
And then I'm going to give the object type as object object mapper over here.
And then equal to new object mapper.
And then we are going to be adding some modules because we are going to be dealing with the local data.
So for the local date parsing and everything to work, what we need to do is we need to register a module.
So in this case we can do I believe we need to provide an instance variable name.
There you go.
And then we are going to call register module.
So in the register module we're going to call new Java type module.
And then we can configure the serialization feature.
So serialization feature needs to be serialization feature and write data with timestamp or write dates
as timestamp.
And then I'm going to provide this flag value as false.
There you go.
So this is a configuration or a basic Jackson configuration that's needed for performing serialization
and deserialization on the Java objects.
In this case, we're going to use the object mapper.
So in here what we can do is after this is done we can do object mapper dot read value.
So in this case it's going to be read value.
And then we're going to get the response body.
So that's exactly what we need.
So in this case response dot body.
And then the value is going to be movie dot class okay.
And then we'll go ahead and return this one.
There you go.
So this is going to take care of performing the string conversion to a movie object and then return
that value.
So just in case if you want to see what's happening in the loggers, what we can do is we can print
the status code over here.
So in this case and we can add the value as status code as.
And then we can call response dot status code.
So this should print the value in the console okay.
So in the case of an error what we can do is we can throw a runtime exception, throw new runtime exception
and then pass the exception into it.
There you go.
So we have successfully built our HTTP client.
Now I'd like to give you a quick recap of what we did.
So the very first thing that we did was we created an instance of the HTTP client.
The way you do it is by calling this new Builder.build and then provide some additional properties to
it.
So this returns you an instance of HTTP client.
So once that is done, what we have after that is the send function call or send function call.
What this call accepts is it accepts a request which is of type HTTP request and then the response body
handler.
So and then what it returns is a HTTP response.
So now from the response body we want to convert that to a movie object.
That's exactly what we are doing over here.
So we are doing that by using the object mapper.
Object mapper is part of the Jackson Library.
And we have created an instance of object mapper over here.
So we have the client ready in the next lecture.
What we'll do is we'll go ahead and test this one by writing a JUnit test case.
I hope you all were able to follow along and create this HTTP client.
In the next lecture we'll go ahead and test this one.
This marks the end of this lecture.
Thank you for watching.