반응형
Notice
Recent Posts
Recent Comments
«   2024/11   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Archives
Today
Total
관리 메뉴

차근차근

[Firebase Cloud Firestore] 스프링 파이어베이스 데이터 베이스 연동 예제 본문

개발

[Firebase Cloud Firestore] 스프링 파이어베이스 데이터 베이스 연동 예제

철산92 2020. 9. 26. 19:25
반응형

1. 파이어베이스 데이터 베이스 생성

 

2. 접속 KEY생성

키 JSON파일 다운로드

3.자바 소스 작성

 -pom.xml dependencies 추가

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

-Key json파일 패키지에 이동

-BtsFireBase.java

package egovframework.com.firebase;

 

import java.io.File;

import java.io.FileInputStream;

 

import com.google.api.core.ApiFuture;

import com.google.auth.oauth2.GoogleCredentials;

import com.google.cloud.firestore.DocumentReference;

import com.google.cloud.firestore.DocumentSnapshot;

import com.google.cloud.firestore.Firestore;

import com.google.cloud.firestore.WriteResult;

import com.google.firebase.FirebaseApp;

import com.google.firebase.FirebaseOptions;

import com.google.firebase.cloud.FirestoreClient;

 

public class BtsFireBase {

 

    public static void main(String[] args) {

        // TODO Auto-generated method stub

        initialize();

 

        try {

            //삽입 

            //BtsVideoVO btsVideoVO = new BtsVideoVO();

            //btsVideoVO.setAge(1);

            //btsVideoVO.setId("test");

            //btsVideoVO.setName("testName");

            //btsVideoVO.setTel("010-1234-1234");

            //insertMember(btsVideoVO);

            

            //호출

            getMemberDetail("test");

        } catch (Exception e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

 

    }

 

 

    public static void initialize() {

        try {

            String path = BtsFireBase.class.getResource("").getPath();

 

            FileInputStream serviceAccount = new FileInputStream(path+"serviceAccountKey.json");

            FirebaseOptions options = new FirebaseOptions.Builder()

                                            .setCredentials(GoogleCredentials.fromStream(serviceAccount))

                                            .setDatabaseUrl("FireBase Console에 사용자 계정에서 가린 DataBase URL삽입")

                                            .build();

            FirebaseApp.initializeApp(options);

            System.out.println("성공");

 

            } catch (Exception e) {

 

                e.printStackTrace();

 

            }

 

    }

 

    public static final String COLLECTION_NAME="member";

 

    public static String insertMember(BtsVideoVO member) throws Exception {

 

           Firestore firestore = FirestoreClient.getFirestore();

 

           ApiFuture<WriteResult> apiFuture = firestore.collection(COLLECTION_NAME).document(member.getId()).set(member);

 

           return apiFuture.get().getUpdateTime().toString();

 

    }

 

    public static void getMemberDetail(String id) throws Exception {

 

        Firestore firestore = FirestoreClient.getFirestore();

        DocumentReference documentReference = firestore.collection(COLLECTION_NAME).document(id);

        ApiFuture<DocumentSnapshot> apiFuture = documentReference.get();

        DocumentSnapshot documentSnapshot = apiFuture.get();

        //System.out.println(documentSnapshot.toString());

 

        BtsVideoVO member = null;

 

        if(documentSnapshot.exists()) {

 

                member = documentSnapshot.toObject(BtsVideoVO.class);

                System.out.println(member.toString());

 

        } else {

 

 

        }

 

 }

 

}

-BtsVideoVO.java

package egovframework.com.firebase;

 

public class BtsVideoVO {

    String id;

    String name;

    int age;

    String tel;

 

    @Override

    public String toString() {

        return "BtsVideoVO [id=" + id + ", name=" + name + ", age=" + age + ", tel=" + tel + "]";

    }

    public String getId() {

        return id;

    }

    public void setId(String id) {

        this.id = id;

    }

    public String getName() {

        return name;

    }

    public void setName(String name) {

        this.name = name;

    }

    public int getAge() {

        return age;

    }

    public void setAge(int age) {

        this.age = age;

    }

    public String getTel() {

        return tel;

    }

    public void setTel(String tel) {

        this.tel = tel;

    }

 

}

 

4. 테스트 결과

반응형
Comments