Skip to content

Latest commit

 

History

History
127 lines (97 loc) · 6.55 KB

README.md

File metadata and controls

127 lines (97 loc) · 6.55 KB

CircleCI Docker Automated build

Sample Spring Boot API using Remote MongoDB Atlas

Topics Covered

This sample project aims to teach you:

  • How to structure a Java Spring Boot API
  • How to design an API contract (i.e. set HTTP response codes, develop routes, etc)
  • What the following components are responsible for: Controller, Service, Repository
  • How to utilize a remote MongoDB cluster
  • How to set up Swagger / Open API spec, an Interface Description Language for describing RESTful APIs

Guides

Software

Software Version Required MacOS Guide Notes
OpenJDK 17.0.4 true How to setup openjdk via Homebrew If you are using an older version of openjdk (minimum v11+), you can still run this project by either setting VM options in the Run Config or appending the following to the bash command below: -Djdk.tls.client.protocols=TLSv1.2
Apache Maven 3.8.6 true Install maven via Homebrew Understanding Apache Maven - The Series
MongoDB 6.0 false Install mongodb-community via Homebrew Use an embedded version of MongoDB. More info under the database related sections.

Codebase

  • Controllers: Manages all the REST calls and status codes
  • Services: The business logic layer that handles any manipulation of data required
  • Repositories: Uses a Java Persistence API (JPA) that analyzes all the methods defined by an interface and automatically generates queries from the method names, in order to simplify the connection to the database from the Service layer
  • Configs: Sets up the configurations for the REST calls, web security, Swagger documentation, etc
  • Models: Defines the structure of all the data objects

Setup and Connect to the Database

Option 1 - Setup and Run Local MongoDB

Option 2 - Setup and Connect to Remote MongoDB

How the Database Populates

  • The database (i.e. MongoDB Atlas cluster) is populated once you initialize the backend.

  • The API used to populate the database is the Latest Stock API: https://rapidapi.com/suneetk92/api/latest-stock-price/

  • The RestTemplateConfig.java makes the connection to the API.

  • The code snippet that populates the database and refreshes the data once every minute is in StockServiceImpl.java as:

    @Scheduled(fixedRate = 60000)
    public void populateStockDatabase() throws StocksResponseException { ... }

Proxy Config

If you are running application and need to get around a proxy, then you must configure this project to use your system proxy. In the ApiApplication.java class, add the proper host and port in the main function.

public static void main(String[] args) {
    Properties props = System.getProperties();
    props.put("https.proxyHost", "your-https-proxy-host");
    props.put("https.proxyPort", "your-https-proxy-port");
    SpringApplication.run(ApiApplication.class, args);
}

Run API via IDE

Run API via Command Line

  • If you don't want to run the application from the IDE, then you can run it from a unix emulator (i.e. Git Bash, Cmder) or Terminal (MacOS).
  • Run the following bash commands in the root directory of this project.
  • Note: The maven binaries have been included so that you may run the mvnw commands even if you don't have maven installed on your $PATH

Windows

mvnw.cmd clean install
mvnw.cmd spring-boot:run -Dspring-boot.run.arguments=--spring.profiles.active=local

MacOS

./mvnw clean install
./mvnw spring-boot:run -Dspring-boot.run.arguments=--spring.profiles.active=local

Swagger

Spring Actuator Endpoints