WEBVTT

00:00.050 --> 00:00.620
Hi everyone.

00:00.620 --> 00:01.250
Welcome back.

00:01.250 --> 00:04.580
In this lecture we will go and learn about the new HTTP client.

00:04.580 --> 00:06.230
That's part of modern Java.

00:06.260 --> 00:09.260
I'll start with some theory first and then we'll get to the coding part.

00:09.260 --> 00:13.790
The new HTTP client API got released in Java 11.

00:13.820 --> 00:21.320
The nicest thing about this particular HTTP client is that it has a support for HTTP two and websockets

00:21.350 --> 00:22.340
on top of it.

00:22.340 --> 00:28.820
This client also has the ability to build HTTP clients in synchronous and asynchronous mode.

00:28.820 --> 00:32.210
If you have questions on what does it mean, don't worry about it.

00:32.240 --> 00:34.940
We will be coding both of these types in action.

00:34.970 --> 00:36.260
That's all about the theory part.

00:36.290 --> 00:40.850
With this information, let's jump right in and start coding this HTTP two client.

00:40.850 --> 00:42.410
So I'm back in IntelliJ over here.

00:42.410 --> 00:45.680
So we have this J web server still up and running.

00:45.710 --> 00:50.990
If I go to the browser and then try to hit this endpoint like if I go to movies dot JSON, it still

00:50.990 --> 00:52.280
gives me this response.

00:52.280 --> 00:58.400
And then if I go to the movies by id JSON, this still responds with a valid movie JSON.

00:58.400 --> 01:03.180
So to test our HTTP client, we need to have this J web server up and running.

01:03.180 --> 01:04.500
So I'm back in IntelliJ here.

01:04.500 --> 01:06.840
So we are going to be working on the HTTP client.

01:06.870 --> 01:07.080
Right.

01:07.110 --> 01:13.080
So you will notice the HTTP package under the explorer jar latest Java features project.

01:13.110 --> 01:14.190
So let's go and open that.

01:14.190 --> 01:18.030
And then you will see a representation of our domain class named movie.

01:18.030 --> 01:22.500
So this is a representation of all the properties we have seen in the JSON response.

01:22.500 --> 01:28.620
So if you go to the browser again so the browser has or the JSON has the movie ID name, let's zoom

01:28.620 --> 01:29.310
in a little bit.

01:29.310 --> 01:33.360
So we have the movie ID name here cast and release date.

01:33.360 --> 01:36.510
So this is exactly what we have in this record class.

01:36.510 --> 01:40.890
So what we are going to do is we are going to be making a call to that service, and then we are going

01:40.890 --> 01:43.710
to be parsing the response and map it to this object.

01:43.740 --> 01:44.130
Okay.

01:44.160 --> 01:46.410
So let's go back to the HTTP client.

01:46.410 --> 01:49.140
In this case we have a class named movies client.

01:49.140 --> 01:54.600
So we are going to be working on building functions that's going to interact with these two services.

01:54.600 --> 01:58.260
So in order to save some time what I did was I have created these two constants.

01:58.260 --> 02:04.450
So the first one actually the URL that gives you all the ten movies, that is part of this JSON and

02:04.450 --> 02:07.240
this is the one which is movie id by URL.

02:07.240 --> 02:13.090
This actually calls this movie by id JSON, which actually returns just one single object.

02:13.120 --> 02:13.450
Okay.

02:13.480 --> 02:17.620
So now with this information, what we will do is as part of this lecture, we are going to build a

02:17.620 --> 02:21.430
HTTP client that's going to be interacting with this particular URL.

02:21.430 --> 02:25.000
So the very first thing here is that we are going to create a function which is going to be public.

02:25.000 --> 02:27.850
And this function is going to be returning a movie object.

02:27.880 --> 02:28.150
Okay.

02:28.180 --> 02:32.440
And then the movie function name is going to be get movie by ID.

02:32.650 --> 02:32.830
Okay.

02:32.860 --> 02:36.250
This function is not going to get any method parameters.

02:36.250 --> 02:37.810
It's going to be a simple function.

02:37.810 --> 02:41.320
But this function is going to interact with this particular URL.

02:41.320 --> 02:43.000
So what do we need here right now.

02:43.000 --> 02:44.290
So we need the client.

02:44.290 --> 02:48.220
And then we need the request object using which we can actually pass the URL.

02:48.220 --> 02:50.110
So very first thing is how do we build a client.

02:50.110 --> 02:52.780
So we can actually build a client as a class variable.

02:52.780 --> 02:55.390
So in this case we can call this one as HTTP client.

02:55.390 --> 02:57.970
So HTTP client is part of the Java dot.

02:57.970 --> 02:59.830
Net dot http package.

02:59.830 --> 03:03.610
And then let's name this one as client okay.

03:03.640 --> 03:09.180
And this client has a builder pattern using which we can build a HTTP client.

03:09.180 --> 03:12.090
So in this case so this is the builder right.

03:12.090 --> 03:17.160
And then let's set up a connection timeout just to show you what are the different options that we have.

03:17.160 --> 03:18.960
So in this case we press Dot here.

03:18.960 --> 03:21.540
It gives you all these different options we have.

03:21.570 --> 03:24.450
But in here I'm going to keep this example really simple.

03:24.450 --> 03:27.270
What I'm going to do I'm going to set up a connection timeout.

03:27.300 --> 03:31.050
So in this case it's going to be duration of seconds.

03:31.050 --> 03:33.270
And the number of seconds is going to be two seconds.

03:33.270 --> 03:36.570
So I'm setting up a connection timeout value to be two seconds.

03:36.570 --> 03:38.520
So using the client what can we do.

03:38.550 --> 03:42.090
Let's use this client over here and then press Dot over here.

03:42.120 --> 03:44.250
It's going to give you different options.

03:44.250 --> 03:48.030
So in this case you notice this send and send async.

03:48.030 --> 03:51.960
So send is basically the synchronous way of building the HTTP client.

03:51.960 --> 03:57.360
What does this mean is that when you make this call the calling thread will be blocked until the response

03:57.390 --> 03:58.740
is received from the service.

03:58.740 --> 04:01.260
And then we continue with the code execution.

04:01.260 --> 04:02.070
Send async.

04:02.100 --> 04:05.580
As you can see, the response is going to be of type completablefuture.

04:05.580 --> 04:08.800
So that's the asynchronous way of building a HTTP client.

04:08.800 --> 04:13.300
So we're going to start with a synchronous approach because that is very simple and straightforward.

04:13.300 --> 04:15.640
So we're going to be making a call to this one.

04:15.640 --> 04:19.270
And if you go to the function what it accepts is a HTTP request.

04:19.270 --> 04:23.320
So what we are going to build is we are going to build the HTTP request instance.

04:23.320 --> 04:26.260
And then we need to provide a body handler also.

04:26.290 --> 04:26.590
Okay.

04:26.620 --> 04:31.960
So now I'm going to create a function because we need a HTTP request for different URLs.

04:31.960 --> 04:38.800
So I'm going to create a generic function so that we can use it for both the movies by id URL and all

04:38.800 --> 04:39.520
movies URL.

04:39.550 --> 04:41.410
So I'm going to create a static function.

04:41.410 --> 04:44.800
It's going to be public static HTTP request.

04:44.800 --> 04:48.850
So that's a response or return type of this particular function.

04:48.880 --> 04:53.020
And then I'm going to give a name of this function as request builder okay.

04:53.050 --> 04:55.450
And this function is going to accept a URL.

04:55.480 --> 04:57.250
So in this case string URL.

04:57.280 --> 04:57.730
There you go.

04:57.760 --> 05:00.940
And then we are going to be calling the HTTP request.

05:00.940 --> 05:03.820
So this also have a builder pattern implemented.

05:03.820 --> 05:05.950
So HTTP request dot new builder.

05:05.950 --> 05:10.680
And then you can actually pass the function or the type of HTTP call you want to make.

05:10.710 --> 05:14.580
So in this case it's going to be the get call and then dot build okay.

05:14.610 --> 05:15.780
But this is not enough right.

05:15.780 --> 05:18.780
We need to provide which URL we are going to be interacting with.

05:18.810 --> 05:20.910
So in this case it's going to be URI.

05:20.940 --> 05:23.190
So you can call URI create.

05:23.190 --> 05:25.650
And then we can provide the URL over here.

05:25.650 --> 05:31.200
So this is going to create a HTTP request for any kind of URL that you are going to be calling this

05:31.200 --> 05:31.890
function with.

05:31.890 --> 05:38.700
So in this case we can actually call request builder function and then pass the URL as movie by id url.

05:38.700 --> 05:42.360
So in this case this is going to give you a response as a request.

05:42.360 --> 05:44.910
So this is going to be request over here.

05:45.090 --> 05:45.840
There you go.

05:45.840 --> 05:49.260
And then we are going to be passing that to this one okay.

05:49.290 --> 05:51.930
And after that what is the response body handler.

05:51.930 --> 05:59.940
So if you take a look at HTTP response and then we can actually call dot body handlers.

06:00.420 --> 06:02.730
And then you can actually call off string.

06:02.730 --> 06:04.980
So I'm going to pass the response as a string.

06:05.010 --> 06:11.490
And then we can use some serialization and deserialization library to actually convert that to a movie

06:11.520 --> 06:12.120
object.

06:12.150 --> 06:13.170
Okay, there you go.

06:13.170 --> 06:14.670
And what this is going to return you.

06:14.670 --> 06:17.010
So this is going to return your HTTP response.

06:17.010 --> 06:20.940
So I'm going to name this one as where response equal to this one.

06:20.940 --> 06:25.890
And then I believe we need to add some kind of exception handler over here.

06:25.920 --> 06:30.480
There you go I'm going to have a generic exception block so that we don't have to deal with specific

06:30.480 --> 06:31.950
exceptions at the moment okay.

06:31.980 --> 06:35.430
And then I'm going to add an error to print the actual exception.

06:35.430 --> 06:38.730
But in reality if you are using a logging library you can use a logger.

06:38.760 --> 06:41.760
There are many options available to handle this one.

06:41.790 --> 06:47.040
Now the next option is I want to parse this response object, which is actually returning the response

06:47.040 --> 06:47.790
body as a string.

06:47.790 --> 06:48.240
Right.

06:48.240 --> 06:50.760
We want to convert that to a movie object.

06:50.760 --> 06:52.410
So how can we achieve that?

06:52.410 --> 06:57.180
There are many deserialization and serialization libraries available, but for this specific course

06:57.180 --> 06:59.190
I'm planning to use the Jackson mapper.

06:59.190 --> 07:03.330
So if you go to the build.gradle file over here you will notice this Jackson libraries.

07:03.330 --> 07:10.230
So this Jackson libraries has some API's or library code using which we can convert a string JSON into

07:10.230 --> 07:13.900
a Java object or a Java object to a string JSON.

07:13.900 --> 07:17.620
So in our case we want to convert a string JSON to a Java object.

07:17.620 --> 07:23.140
So I'm going to create an instance of object mapper so that we can use it across this specific class.

07:23.140 --> 07:27.790
So in here let's go ahead and create an object mapper instance private final.

07:27.790 --> 07:31.780
And then I'm going to give the object type as object object mapper over here.

07:31.780 --> 07:34.690
And then equal to new object mapper.

07:34.690 --> 07:39.850
And then we are going to be adding some modules because we are going to be dealing with the local data.

07:39.850 --> 07:44.350
So for the local date parsing and everything to work, what we need to do is we need to register a module.

07:44.380 --> 07:47.890
So in this case we can do I believe we need to provide an instance variable name.

07:47.890 --> 07:48.490
There you go.

07:48.520 --> 07:50.740
And then we are going to call register module.

07:50.740 --> 07:54.490
So in the register module we're going to call new Java type module.

07:54.490 --> 07:57.910
And then we can configure the serialization feature.

07:57.910 --> 08:05.620
So serialization feature needs to be serialization feature and write data with timestamp or write dates

08:05.620 --> 08:07.270
as timestamp.

08:07.300 --> 08:09.880
And then I'm going to provide this flag value as false.

08:10.030 --> 08:10.480
There you go.

08:10.480 --> 08:16.680
So this is a configuration or a basic Jackson configuration that's needed for performing serialization

08:16.680 --> 08:18.660
and deserialization on the Java objects.

08:18.660 --> 08:20.700
In this case, we're going to use the object mapper.

08:20.700 --> 08:26.910
So in here what we can do is after this is done we can do object mapper dot read value.

08:26.940 --> 08:29.160
So in this case it's going to be read value.

08:29.160 --> 08:31.080
And then we're going to get the response body.

08:31.080 --> 08:32.460
So that's exactly what we need.

08:32.460 --> 08:34.650
So in this case response dot body.

08:34.650 --> 08:38.580
And then the value is going to be movie dot class okay.

08:38.580 --> 08:41.100
And then we'll go ahead and return this one.

08:41.250 --> 08:41.730
There you go.

08:41.730 --> 08:47.310
So this is going to take care of performing the string conversion to a movie object and then return

08:47.310 --> 08:47.910
that value.

08:47.910 --> 08:52.200
So just in case if you want to see what's happening in the loggers, what we can do is we can print

08:52.200 --> 08:53.550
the status code over here.

08:53.550 --> 08:57.600
So in this case and we can add the value as status code as.

08:57.630 --> 09:00.810
And then we can call response dot status code.

09:00.810 --> 09:03.000
So this should print the value in the console okay.

09:03.030 --> 09:08.460
So in the case of an error what we can do is we can throw a runtime exception, throw new runtime exception

09:08.460 --> 09:10.080
and then pass the exception into it.

09:10.260 --> 09:10.800
There you go.

09:10.800 --> 09:13.800
So we have successfully built our HTTP client.

09:13.830 --> 09:16.420
Now I'd like to give you a quick recap of what we did.

09:16.420 --> 09:20.770
So the very first thing that we did was we created an instance of the HTTP client.

09:20.770 --> 09:26.260
The way you do it is by calling this new Builder.build and then provide some additional properties to

09:26.260 --> 09:26.410
it.

09:26.410 --> 09:29.440
So this returns you an instance of HTTP client.

09:29.440 --> 09:35.260
So once that is done, what we have after that is the send function call or send function call.

09:35.260 --> 09:41.860
What this call accepts is it accepts a request which is of type HTTP request and then the response body

09:41.890 --> 09:42.400
handler.

09:42.400 --> 09:45.310
So and then what it returns is a HTTP response.

09:45.310 --> 09:49.990
So now from the response body we want to convert that to a movie object.

09:49.990 --> 09:52.120
That's exactly what we are doing over here.

09:52.120 --> 09:54.610
So we are doing that by using the object mapper.

09:54.640 --> 09:57.070
Object mapper is part of the Jackson Library.

09:57.070 --> 10:01.000
And we have created an instance of object mapper over here.

10:01.000 --> 10:03.610
So we have the client ready in the next lecture.

10:03.610 --> 10:08.800
What we'll do is we'll go ahead and test this one by writing a JUnit test case.

10:08.830 --> 10:12.520
I hope you all were able to follow along and create this HTTP client.

10:12.520 --> 10:15.010
In the next lecture we'll go ahead and test this one.

10:15.040 --> 10:16.420
This marks the end of this lecture.

10:16.420 --> 10:17.620
Thank you for watching.
