- Host a Spring Boot application for free on Render
- Prerequisites
- Project setup
- Initialize GitHub repository
- Create a customer model
- Create a customer controller
- Package the application
- Run and test the Jar file
- Create a Dockerfile
- Push the project to GitHub
- Connect repository on Render
- Enter application name
- Select instance type
- Deploy the application
- Test the web service
- Conclusion
Host a Spring Boot application for free on Render
Most software engineers spend their free time working on side projects. These projects are either aimed at realizing an idea or improving their skills.
Hosting these applications once the development is complete is always a good practice. This helps you to showcase your skills to potential clients.
For applications that do not have a business model, there is no need of spending money to host the apps. There are many platforms that provide free hosting services.
In this tutorial, you will learn how to host a spring boot application for free on Render using a Dockerfile. This application will be a simple REST API that returns a list of customers.
Prerequisites
Project setup
To create a new project, go to Spring initializr and select Maven in the Project section. Select Java in the Language section and 3.x.x as the version in the Spring Boot section.
The x in the version represents the latest version of Spring Boot. In the Project Metadata section, enter the respective sections as shown below.
Press the ADD DEPENDENCIES button and add the dependencies Spring Web and Lombok. The project structure of your application should be as shown below.
- Spring Web – A library of the spring framework runtime for creating web applications.
- Lombok – A library that helps you reduce boilerplate code by generating helper methods.
Press the GENERATE button to download a ZIP file containing your project. Unzip the file on your computer and import the extracted folder in IntelliJ.
Initialize GitHub repository
You must initialize the project to a Git repository since you are going to push it to GitHub.
Open a new terminal window and cd into your project. Execute the following command to initialize it to a GitHub repository.
C:\Users\redhat\Documents\Projects\App> git init
Create a customer model
In IntelliJ, create a package named customer in the package src/main/java/com/javawhizz/App.
Create a file named Customer.java in the new package and copy and paste the following code into the file.
package com.javawhizz.App.customer; import lombok.AllArgsConstructor; import lombok.Data; @Data @AllArgsConstructor public class Customer
In this file, you have created a class named customer and you will use this class to create customers. The @Data annotation generates the getter and setter methods. The @AllArgsConstructor generates an instance of a customer having all the fields.
Create a customer controller
Create a file named CustomerController.java in the customer package. Copy and paste the following code into the file.
package com.javawhizz.App.customer; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController @RequestMapping("api/v1/customers") public class CustomerController < private static final ListCUSTOMERS = List.of( new Customer(1L, "john", "doe", "john@javawhizz.com"), new Customer(2L, "mary", "public", "mary@javawhizz.com"), new Customer(3L, "elon", "musk","elon@javawhizz.com"), new Customer(4L, "dunny","duncan","dunny@javawhizz.com") ); @GetMapping public List findAllCustomers() < return CUSTOMERS; >>
In this file, you have created a class for handling the HTTP requests made to your application. The @RequestMapping annotation sets the URI of the base path to api/v1/customers.
The findAllCustomers() method handles the GET requests to the application. The @GetMapping indicates that this method handles GET requests. The method returns a collection of customers defined using the List.of() method.
Package the application
A folder named target is the default folder for the Jar file. You should remove the target references in the .gitignore file.
If you do not remove the references, the target folder is not uploaded to GitHub. This means that Render will not find your Jar file during the hosting process.
Open the .gitignore file in the project root and remove the following references.
target/ !.mvn/wrapper/maven-wrapper.jar !**/src/main/**/target/ !**/src/test/**/target/
In Intellij, select View > Tool Windows > Maven to open the maven build tool for Intellij. On the panel that opens on the left side of IntelliJ, select App > Lifecycle.
To package the application, double-click on the package section under Lifecycle. The following image shows how to package the application.
This will package your application and create a Jar file named App-0.0.1-SNAPSHOT.jar.
Run and test the Jar file
Open a new terminal window and cd into the target folder of your project. To run the Jar file, execute the following command.
C:\Users\redhat\Documents\Projects\App\target> java -jar App-0.0.1-SNAPSHOT.jar
If your application starts without any errors, go to localhost:8080/api/v1/customers to test the API. The API returns a JSON response containing an Array of customers.
Create a Dockerfile
Create a file named Dockerfile in the project root. Copy and paste the following Docker commands into the file.
FROM eclipse-temurin:17-jdk-alpine VOLUME /tmp COPY target/*.jar app.jar ENTRYPOINT ["java","-jar","/app.jar"] EXPOSE 8080
These are the commands executed when hosting the application on render. The following is a description of the task performed by the Docker commands.
- FROM – sets the base image for building the image for your application.
- VOLUME – creates a mount point
- COPY – copies the jar file in the target folder to a file named app.jar
Push the project to GitHub
Go to your GitHub account and create a repository named app. Note the commands provided after creating the repository.
Open a new terminal window and follow the commands provided on your repository to push to GitHub. If your application should be as shown below. Ensure you can see the target folder.
Connect repository on Render
To connect the repository on Render, Go to the Dashboard section of Render and toggle the New button.
Since we want to deploy a dynamic application, press on Web Services section. Enter the URL of the GitHub repository in the Public Git repository section. Press the continue button to connect the repository.
Enter application name
After connecting your repository, enter the name of the application as App, on the page that opens. Let the Region, Branch, Root Directory, and Runtime remain to default.
Select instance type
Scroll down the page and ensure you have selected the Free instance type to avoid charges.
Deploy the application
To deploy the application, press the Create Web Service button. This downloads the base image then builds your image and finally deploys it to the registry. Your application should be as shown below if it there were no errors during deployment.
The green button indicates that the application is live. The base URL of the application is on the top left side of the page below the application name.
Test the web service
Copy and paste the base URL into the search bar of your browser. To get the Array of customers, ensure you have added the path /api/v1/customers after the base path.
Here is my full URI. Your application should return the following response.
Conclusion
In this tutorial, you have learned how to host a Spring Boot application for free on Render.
Changes deployed to the main branch get updated in your application. This means you only deploy the application once and add features as you go using commits.
Add your custom changes and test how the application works. You can also change the application to fetch the collection of customers from a database.
The code for this project is available on GitHub. Visual learners are also not left out, the video for this tutorial is available on YouTube.