Build an AirBNB API by SpringBoot and MongoDB Atlas
In this post, we will review how we build a full-blown API using SpingBoot and fetching the data set of AirBNB from MongoDB Atlas and enhancing its data by adding the temperature of that city from a third-party API.
This is the system diagram for our application:
Part 1
In this video I explain the basics of an API which includes an intro to HTTP, REST, JSON, rest client, and finally SpringBoot and Spring Framework.
Part 2
In the second lecture, we start doing some code: building a rest web service using SpringBoot. We will see how fast and easy it can be.
Build a Get REST service
1- mark your class as REST controller class with @RestController
package com.airbnbjavaworkshop.airbnbapi.controllers;
import com.airbnbjavaworkshop.airbnbapi.entity.Listing;
public class ListingController {
Listing returnListing() {
return new Listing(10,"queen");
}
}
2-Mark your method as a REST endpoint with @GetMapping([your endpoint here])
package com.airbnbjavaworkshop.airbnbapi.controllers;
import com.airbnbjavaworkshop.airbnbapi.entity.Listing;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ListingController {
@GetMapping("/listing")
Listing returnListing() {
return new Listing(10,"queen");
}
}
@PostMapping("/listing")
Listing addListing(@RequestBody Listing newListing) {
dataRepository.addNewListing(newListing);
return newListing;
}
public class DataRepository {
List<Listing> list = new ArrayList<Listing>();
public void addNewListing(Listing listing){
list.add(listing);
}
public Listing returnFirstListing(){
return list.get(0);
}
public List<Listing> returnAllListing(){
return list;
}
}
return all listing
@GetMapping("/listingAll")
List<Listing> returnAllListing() {
return list;
}
Spring Framework
Most popular enterprise software framework Wiring up different components based on different interfaces built by teams unaware of each other. 1- The main goal of the Spring framework is how to decouple the relationship between dependent Object 2- Dependency Injection means to separate the class which is dependent and inject them when it's needed from outside.
Dependency Injection
“Dependency Injection” is a 25-dollar term for a 5-cent concept.
the code to be added here
can be applied to all of these :
Variables Dependencies instance variables Class member variable
public class Example {
private DatabaseThingie myDatabase;
public Example() {
myDatabase = new DatabaseThingie();
}
public Example(DatabaseThingie useThisDatabaseInstead) {
myDatabase = useThisDatabaseInstead;
}
public void DoStuff() {
...
myDatabase.GetData();
...
}
}
A Drawing Application
Polyphormisem
Inversion of Control
Implementing Dependency Injection by inverting the control of dependency management
Spring Context Spring IoC container
Spring contexts are also called Spring IoC containers, which are responsible for instantiating, configuring, and assembling beans by reading configuration metadata from XML, Java annotations, and/or Java code in the configuration files.
What are Spring Beans?
Spring annotations
@Autowired - marks a constructor, field, or setter method to be autowired by Spring dependency injection. @SpringBootApplication - enables Spring Boot autoconfiguration and component scanning. @Repository - indicates that an annotated class is a repository, which is an abstraction of data access and storage.
Part 3
Third lecture: connect to MongoDB and retrieve data
Part 4
Fourth lecture: in this video, we enrich the data by retrieving countries' information from the Restcountries service.