Pages

Sunday, 29 November 2020

Java | Spring Boot Project | Firebase For Database | GitHub | Heroku To Deploy Project LIVE

STEP 1: Create Spring Boot Web Project

From your IDE:

  • Create New Spring Starter Project

  • Select Web

  • Create packages:

com.example.demo com.example.demo.controller com.example.demo.dbinit com.example.demo.model com.example.demo.service

STEP 2: Initialize Firebase

Create class in dbinit package:

@Service public class InitializeFirebase { @PostConstruct public void initializeFirebase() throws IOException { } }

@PostConstruct ensures the method runs automatically on app startup.


STEP 3: Firebase Login & Firestore Setup

  • Open Firebase Console

  • Choose Cloud Firestore

  • Click Start Collection

  • Collection ID: beginner

  • Document ID: anything (e.g., xyz123)

  • Field example:
    id = "xyz123"

Document ID can be auto-generated or same as your model's ID.


STEP 4: Firestore Connection Code

Go to:

Project Settings → Service Accounts → Java → Generate Code

Copy the code snippet provided.


STEP 5: Initialize Firebase in Project

Paste code inside initializeFirebase():

@PostConstruct public void initializeFirebase() throws IOException { FileInputStream serviceAccount = new FileInputStream("AAAAAAA.json"); FirebaseOptions options = new FirebaseOptions.Builder() .setCredentials(GoogleCredentials.fromStream(serviceAccount)) .setDatabaseUrl("https://BBBBBBB.com") .build(); FirebaseApp.initializeApp(options); }

STEP 6: Add Firebase Dependencies

  • Go to Maven Repository

  • Search firebase-admin

  • Use a compatible version (example: 6.11.0)

  • Add in pom.xml

  • Import Firebase classes


STEP 7: Generate Private Key

In Service Accounts:

  • Click Generate New Private Key

  • Download the .json file

  • Paste it inside the project root

  • Replace:

FileInputStream serviceAccount = new FileInputStream("./<filename>.json");

STEP 8: Create Model – Student

Fields:

id name age place

Generate:

  • constructors

  • getters/setters

  • toString()


STEP 9: Create home.jsp

Path: src/main/webapp/home.jsp

<form action="/save"> id: <input type="text" name="id" value="1000"><br> name: <input type="text" name="name"><br> age: <input type="text" name="age"><br> place: <input type="text" name="place"><br> <input type="submit" value="save"> </form>

STEP 10: Controller Setup

@Controller public class StudentController { @RequestMapping("/") public String home() { return "home.jsp"; } }

STEP 11: Add Tomcat Jasper (for JSP)

If page doesn’t load, add Tomcat Jasper:

  • Search tomcat-embed-jasper

  • Version must match tomcat-embed-core

  • Add dependency to pom.xml


STEP 12: Relaunch Project

Now JSP loads properly at:

http://localhost:8080/

STEP 13: Controller Save Method

@RequestMapping("/save") public String saveStudent(Student student) { System.out.println("successfully saved"); return "home.jsp"; }

Console prints "successfully saved" → Controller working.


STEP 14: Create Service Class

@Service public class StudentService { public static void saveStudent(Student student) { } }

STEP 15: Firebase Code to Save Data

public static void saveStudent(Student student) { Firestore dbFirestore = FirestoreClient.getFirestore(); ApiFuture<WriteResult> future = dbFirestore .collection("beginner") .document(student.getId()) .set(student); }

STEP 16: Call Service From Controller

@RequestMapping("/save") public String saveStudent(Student student) { StudentService.saveStudent(student); System.out.println("successfully saved"); return "home.jsp"; }

STEP 17: Add Data to Firebase

  • Relaunch project

  • Submit form

  • Check Firestore
    → Data stored successfully


STEP 18: Push Project to GitHub

Commands:

cd "<path>" git init git remote add origin <repo-link> # If reinitialized rm -rf .git git init git add . git commit -m "first commit" git branch -M main git push -u origin main

STEP 19: Deploy Spring Boot App to Heroku

  1. Login to Heroku

  2. Create New App
    Example: test

  3. Connect to GitHub

  4. Authorize (first time only)

  5. Select repository

  6. Enable Automatic Deploy

  7. Click Deploy Branch

  8. After deployment → Heroku gives URL

Example:

https://test.herokuapp.com/

Your project is now LIVE.


✔ End

-- Thanks --

1 comment:

Deploy Your Code to Heroku: Complete Guide (GitHub & Node.js)

Heroku provides a smooth workflow for deploying apps directly from GitHub or via Git. In this guide, you’ll learn both methods step-by-step....