- Topics Covered
- Guides
- Software
- Codebase
- Setup and Connect to the Database
- How the Database Populates
- Run API via IDE
- Run API via Command Line
- Swagger
- Spring Actuator Endpoints
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
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. |
- 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
- MacOS Guide: Install mongodb-community via Homebrew
- Windows Guide: Install mongodb via MSI package
-
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 { ... }
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);
}
- Setup local JDK under Project Settings and Structure in IntelliJ
- Create Springboot Run Debug Configuration in IntelliJ
- Under the configuration, there is an attribute called
Active profiles
, set this tolocal
to utilize theapplication-local.yaml
on startup.
- Under the configuration, there is an attribute called
- After you have your environment setup, run the application via the play button in the IDE: How to run the Springboot App in IntelliJ
- 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
mvnw.cmd clean install
mvnw.cmd spring-boot:run -Dspring-boot.run.arguments=--spring.profiles.active=local
./mvnw clean install
./mvnw spring-boot:run -Dspring-boot.run.arguments=--spring.profiles.active=local
- Swagger includes automated documentation that allows you to TRY OUT the API endpoints on the browser!
- OpenAPI spec 3.0 is configured under
SwaggerConfig.java
- Swagger UI: http://localhost:8080/swagger-ui/index.html
- OpenAPI spec 3.0.3: http://localhost:8080/v3/api-docs
- Stock data: http://localhost:8080/api/v1/stocks
- The actuator endpoints are configured under the
application.yaml
file. - Health: http://localhost:8080/health
- Metrics: http://localhost:8080/metrics
- Mappings: http://localhost:8080/mappings