Building a Mood Journal for Friend Groups with Spring Boot + Firebase

Backend BrewBackend Brew
2 min read

Part 1: Project Setup and Firebase Integration

Learn how to set up your Spring Boot project and integrate Firebase for backend storage and authentication.

๐Ÿ”ง What Weโ€™ll Cover:

  • Installing Java, Spring Boot, VS Code setup

  • Creating Spring Boot Project (via Spring Initializr)

  • Setting up Firebase project

  • Adding Firebase Admin SDK to Spring Boot

  • Connecting to Firestore

  • Testing Firestore connection with REST endpoint


โœ… Prerequisites

  • JDK 17 or above installed

  • Gradle or Maven

  • VS Code with Spring Boot Extension Pack

  • Firebase account


๐Ÿ“ฆ Step 1: Create a Spring Boot Project

Go to Spring Initializr:

  • Project: Gradle (or Maven)

  • Language: Java

  • Dependencies:

    • Spring Web

    • Spring Boot DevTools

  • Group: com.moodjournal

  • Artifact: moodjournal

Click Generate โ†’ extract ZIP โ†’ open in VS Code


๐Ÿ”Œ Step 2: Set Up Firebase Project

  1. Go to Firebase Console

  2. Create new project: "Mood Journal"

  3. Add a Web App (you can skip hosting)

  4. In Project Settings > Service Accounts:

    • Click Generate new private key

    • Save the JSON file securely


๐Ÿ” Step 3: Add Firebase Admin SDK

Add to build.gradle

implementation 'com.google.firebase:firebase-admin:9.1.1'

If using Maven:

<dependency>
  <groupId>com.google.firebase</groupId>
  <artifactId>firebase-admin</artifactId>
  <version>9.1.1</version>
</dependency>


โš™๏ธ Step 4: Configure Firebase SDK in Spring Boot

๐Ÿ“ Place your downloaded JSON in src/main/resources/firebase-service-account.json

๐Ÿ“„ Create FirebaseConfig.java:

@Configuration
public class FirebaseConfig {
    @PostConstruct
    public void initialize() {
        try {
            FileInputStream serviceAccount = new FileInputStream("src/main/resources/firebase-service-account.json");

            FirebaseOptions options = FirebaseOptions.builder()
                .setCredentials(GoogleCredentials.fromStream(serviceAccount))
                .setDatabaseUrl("https://<your-project-id>.firebaseio.com")
                .build();

            if (FirebaseApp.getApps().isEmpty()) {
                FirebaseApp.initializeApp(options);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

โœ… Replace <your-project-id> with your actual Firebase project ID


๐Ÿงช Step 5: Test Firestore Connection

Create TestController.java:

@RestController
public class TestController {

    @GetMapping("/test")
    public String testConnection() {
        Firestore firestore = FirestoreClient.getFirestore();
        return "Firestore is working: " + firestore.getOptions().getProjectId();
    }
}

Run the app.

Then visit: http://localhost:8080/test



๐Ÿ”š Wrap-Up

You now have:

  • Connected to Firebase Firestore securely

In the next part, weโ€™ll add user authentication and group creation. Stay tuned!


0
Subscribe to my newsletter

Read articles from Backend Brew directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Backend Brew
Backend Brew