STEP 1: CREATE SPRING BOOT WEB PROJECT
- New Spring Starter Project
- Web
- New packages
- com.example.demo
- com.example.demo.controller
- com.example.demo.dbinit
- com.example.demo.model
- com.example.demo.service
STEP 2: INITIALIZE FIREBASE METHOD
- New class in dbinit
@Service //to treat as a service class
public class InitializeFirebase {
@PostConstruct //to execute after run
public void initializeFirebase() throws IOException {
}
public void initializeFirebase() throws IOException {
}
}
STEP 3: FIREBASE LOGIN
- Go to Console
- Select Cloud Firestore
- Start Collection
- Collection ID
- "beginner"
- Document ID
- xyz123
- id = String | xyz123
- //Document Id and id can be same, else system can autogenerate Document ID
- Collection ID
STEP 4: FIRESTORE CLOUD CODE TO CONNECT
- Click setting icon near Project Overview * (left bar)
- Go to Project Setting
- Service accounts
- Java
- COPY THE CODE
STEP 5: CODE TO INITIALIZE FIREBASE
- Paste code in initializeFirebase() method
@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);
}
new FileInputStream("AAAAAAA.JSON");
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.fromStream(serviceAccount))
.setDatabaseUrl("https://BBBBBBB.com")
.build();
FirebaseApp.initializeApp(options);
}
STEP 6: ADD DEPENDENCIES
- Go to maven Repositories
- Search firebase admin
- Any compatible version Copy the code
- Here 6.11.0 has been taken
- Add code in pom.xml
- Import packages to initialize firebase in STEP 5.
STEP 7: GENERATE NEW PRIVATE KEY
- Switch to STEP 4
- Click on Generate new private key
- Download json file
- Paste it into the project folder
- You can right click on project in IDE then paste it
- Now copy the json file name and paste into
- FileInputStream serviceAccount =
new FileInputStream("./<filename>.JSON");
- FileInputStream serviceAccount =
STEP 8: CREATE MODEL: Student
- id
- name
- age
- place
Now generate
- field constructor
- default constructor
- setters getters
- toString() method
STEP 9: HOME.JSP
- Go to folder src (not in package src but in folder src)
- Create folder webapp
- Create new jsp file
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Student</title>
</head>
<body>
<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>
</body>
</html>
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Student</title>
</head>
<body>
<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>
</body>
</html>
STEP 10: CONTROLLER SETUP
- Create class StudentController in controller package
@Controller
public class StudentController {
@RequestMapping("/")
public String home() {
return "home.jsp";
}
}
public class StudentController {
@RequestMapping("/")
public String home() {
return "home.jsp";
}
}
STEP 11: RUN APP
- Run as Spring Boot App
- Open Browse
- Go to Localhost:8080
- It will download something
- That is jsp page
Actually project doesn't have any library to run jsp
- Go to maven repositories
- Search Tomcat Jasper
- Version should be same as
- In project go to maven dependency library
- Check the version of tomcat-embed-core-9.0.39.jar
- Tomcat Jasper version should be same as tomcat-embed-core version
- Add dependency to pom.xml
STEP 12: RELAUNCH PROJECT AFTER TOMCAT JASPER
- Now page will be visible
id: ________________
name:_____________
age:_______________
place:_____________
<save>
STEP 13: CONTROLLER FOR SAVE
@RequestMapping("/save")
public String saveStudent(Student student) {
System.out.println("successfully saved");
return "home.jsp";
}
- Relaunch Project:
- Refresh Browser
- Click save button
- In console "successfully saved" printed
- So save button working properly
STEP 14: SERVICE CLASS TO SAVE DATA
- Make a service class
@Service
public class StudentService {
public static void saveStudent(Student student) {
public class StudentService {
public static void saveStudent(Student student) {
}
}
}
STEP 15: FIREBASE CODE TO SAVE DATA
- Open firebase
- Go to docs
- Cloud firestore
- Add and manage data
- Add data
- Select java
- Copy the code(only required)
- Paste it into the saveStudent() method (STEP 14)
- Replace
- Collection name
- Document
- Object
public static void saveStudent(Student student) {
Firestore dbFirestore = FirestoreClient.getFirestore();
ApiFuture<WriteResult> future = dbFirestore.collection("beginner").document(student.getId()).set(student);
}
ApiFuture<WriteResult> future = dbFirestore.collection("beginner").document(student.getId()).set(student);
}
STEP 16: CALL SAVESTUDENT() METHOD IN CONTROLLER
@RequestMapping("/save")
public String saveStudent(Student student) {
StudentService.saveStudent(student);
System.out.println("successfully saved");
return "home.jsp";
}
System.out.println("successfully saved");
return "home.jsp";
}
STEP 17: ADD DATA TO FIREBASE CLOUD
- Relaunch project
- Add data
- Open firebase cloud
- Data is added successfully
STEP 18: ADD PROJECT TO GITHUB
- Open github
- Create repository
- Now you have command to use
- You can just follow steps and copy - paste it
- Open gitbase
- Go to the file location
- Enter command in gitbase
- For location right click on project
- Properties
- Resource
- Location
- Copy location path
Commands:
- $ cd "<path>"
- $ git init
- $ git remote add origin http------------ (code copied from git repo)
- If Initialized found
- Ok
- Else if Reinitialized found
- Better to remove
- $ rm -rf .git
- $ git init
- If Initialized found
- $ git add .
- $ git commit -m "first commit"
- Dot(.) means, add all code from current location
- $ git branch -M main
- $ git push -u origin main
Refresh repository
STEP 19: DEPLOY APP TO HEROKU
- Open heroku
- Create new app
- "test" (use required name)
- Connect to github
- Authorized(for first time)
- Heroku and git
- Search repo
- Connect
- Enable automatic deploy
- Deploy branch
App successfully deployes
You will get url
Like "test.herokuapp.com"
-- Thanks --
Great
ReplyDelete