Chris builds upon our application and implement examples of each RxJava construct. He implements an API using the Landon Hotel as an example.
- [Instructor] In this video, we will create our Spring config, we will add a service layer to query the API, then we will add a model to represent the JSON response. Finally, we will print that respond to the command line. Okay, let's get started. Let's navigate to our base package. Right-click, New, Package, config.
Let's create a new class. We can name it ApiConfig. Let's be sure to add a Configuration annotation. Let's be sure we imported the correct annotation. All right, now let's add a ObjectMapper Bean.
Let's use the keyboard shortcut, Alt + Enter, to import the object map or class. Okay, let's instantiate a new ObjectMapper and then let's set it to ignore unknown properties.
This way we won't have to explicitly map every single JSON property. Then, let's add a Bean annotation. Cmd + Alt + L to format. Now, let's add a ObjectWriter Bean. Let's import ObjectWriter using Alt + Enter and be sure to choose the Jackson version.
Okay, let's wire in the ObjectMapper Bean, then let's return a PrettyPrinter from the ObjectMapper. Let's not forget to add a Bean annotation. Finally, let's define a reactive WebClient Bean.
When declaring your WebClient, be sure to capitalize the letter C. Okay, let's be sure to import the reactive WebClient, then let's use the WebClient builder. Let's set the clientConnector to a ReactorClientHttpConnector.
Then finally, let's build our WebClient. Okay, and be sure to add a Bean annotation. Next up, let's add in a package called model, New, Package, model.
Let's add a new class called CoinBasedResponse. Let's declare a innerclass called Data. Let's add in three String fields, base, currency, and amount.
Let's use the keyboard shortcut, Cmd + N, then select Getter and Setter. Hold down Shift and press down twice. This will generate a default Getter and Setter for each field. Okay, great, now let's add a Data field to the CoinBaseResponse.
Then, let's generate a Getter and Setter using Cmd + N. Okay, save and auto format. Now, let's create a package called service. New, Package, service. Okay, now we're gonna create a class called CoinbaseService.
Let's declare it as a interface. Let's add in a method that returns a Mono of the type CoinBaseResponse. Alt + Enter to import and let's name the method getCryptoPrice. Let's pass in a String called priceName.
Now, let's create a class called CoinbaseServiceImpl. Let's implement the CoinbaseService interface, click on the red light bulb, then click on Implement methods. Let's be sure to add in a @Service annotation and then let's autowire in a WebClient.
Let's use the webClient to make a get call to the uri, api.coinbase.com/v2/prices/cryptoName/buy. Let's pass in the parameter priceName. Let's format on each chained method call.
Add the .accept header for the JSON type, then call .exchange and then let's .flatMap the clientResponse to a Mono of the type CoinBaseResponse. Ctrl + space to auto-suggest formatting.
Let's call bodyToMono pass again the CoinbaseService.class. Let's be sure to return that value. Oh, looks like we have a typo. We passed in the wrong class to bodyToMono. Okay, now let's save. Now, let's create a package called cmd.
Let's create a new class called CmdLineUi. Let's annotate this as a Component and implement the ComandLineRunner interface.
Left-click on the red light bulb and click Implement. All right. Now, let's auto-wire our CoinbaseService. Let's call the getCryptoPrice under CoinbaseService. Let's pass in the name of the crypto we are interested in, BTC-USD, for Bitcoin U.S. Dollars.
Let's .subscribe to that Mono. Let's get the Data from that response, then print out the amount. Finally, let's add the annotation EnableAutoConfiguration and also the ComponentScan annotation.
If you'd like to learn more about these annotations, you should check out a course on Spring Boot in our Learning Library. Okay, let's open up the terminal and invoke the Gradle Wrapper using the command bootRun. Okay, it looks like we have an error. In your CoinbaseServiceImpl class, when you define your uri, be sure to surround this parameter with brackets.
In my uri, it looks like I passed in a String literal. I wanted to pass in the actual String parameter. Now, let's try running our application. If you get the error Port 8080 was already in use, you may have to kill that process.
Let's type in kill -9 and then the process ID. Let's try invoking the Gradle Wrapper and the command bootRun. Okay, it looks like we pulled back the value correctly.
- Reactive programming concepts and techniques
- Functional programming concepts and techniques
- Overview of popular reactive frameworks
- Building an RxJava application
- Building an Akka application
- Unit testing with Akka and RxJava