使用Firebase在Spring boot中创建RestApi
我正在尝试使用SpringBoot和Firebase实现“创建”(CRUD的一部分) 我要检索到Firebase的对象如下所示使用Firebase在Spring boot中创建RestApi,spring,firebase,spring-boot,google-cloud-firestore,Spring,Firebase,Spring Boot,Google Cloud Firestore,我正在尝试使用SpringBoot和Firebase实现“创建”(CRUD的一部分) 我要检索到Firebase的对象如下所示 public class Farm { private String id; private String farmer; private String name; private String product; private Byte image; private String description; pr
public class Farm {
private String id;
private String farmer;
private String name;
private String product;
private Byte image;
private String description;
private String location;
private String geopoint;
private int rating;
...
}
我已经为uploadImage和CreateforFarm实现了Api
这些是uploadImage和Create的实现(与uploadImage分开)
@PostMapping(“/upload image”)
公共响应上载映像(@RequestParam(“文件”)MultipartFile文件)引发异常{
返回firebaseService.uploadFile(文件);
}
@后期映射(value=“/createFarm”)
公共字符串addFarm(@RequestBody Farm){
CollectionReference userRef=db.getFirebase().collection(“农场”);
userRef.document(farm.getId()).set(farm);
return farm.getId();
}
上传文件的逻辑
public ResponseEntity<String> uploadFile(MultipartFile multipartFile) throws IOException {
String objectName = generateFileName(multipartFile);
FileInputStream serviceAccount = new FileInputStream(FIREBASE_SDK_JSON);
File file = convertMultiPartToFile(multipartFile);
Path filePath = file.toPath();
Storage storage = StorageOptions.newBuilder().setCredentials(GoogleCredentials.fromStream(serviceAccount)).setProjectId(FIREBASE_PROJECT_ID).build().getService();
BlobId blobId = BlobId.of(FIREBASE_BUCKET, objectName);
BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType(multipartFile.getContentType()).build();
storage.create(blobInfo, Files.readAllBytes(filePath));
return ResponseEntity.status(HttpStatus.CREATED).body("product uploaded successfully");
}
public ResponseEntity上载文件(MultipartFile MultipartFile)引发IOException{
String objectName=generateFileName(多部分文件);
FileInputStream serviceAccount=新的FileInputStream(FIREBASE\u SDK\u JSON);
File File=convertMultiPartToFile(multipartFile);
Path filePath=file.toPath();
Storage Storage=StorageOptions.newBuilder().setCredentials(GoogleCredentials.fromStream(servicecomport)).setProjectId(FIREBASE_PROJECT_ID).build().getService();
BlobId BlobId=BlobId.of(FIREBASE_BUCKET,objectName);
BlobInfo BlobInfo=BlobInfo.newBuilder(blobId).setContentType(multipartFile.getContentType()).build();
创建(blobInfo,Files.readAllBytes(filePath));
返回ResponseEntity.status(HttpStatus.CREATED).body(“产品上传成功”);
}
我正在尝试将这两个方法添加到一起,这样我就可以通过使用postman的调用检索Farm中的所有属性(包括IMAGE)
对于这种情况,谁能给我一些建议或是医生的建议
public ResponseEntity<String> uploadFile(MultipartFile multipartFile) throws IOException {
String objectName = generateFileName(multipartFile);
FileInputStream serviceAccount = new FileInputStream(FIREBASE_SDK_JSON);
File file = convertMultiPartToFile(multipartFile);
Path filePath = file.toPath();
Storage storage = StorageOptions.newBuilder().setCredentials(GoogleCredentials.fromStream(serviceAccount)).setProjectId(FIREBASE_PROJECT_ID).build().getService();
BlobId blobId = BlobId.of(FIREBASE_BUCKET, objectName);
BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType(multipartFile.getContentType()).build();
storage.create(blobInfo, Files.readAllBytes(filePath));
return ResponseEntity.status(HttpStatus.CREATED).body("product uploaded successfully");
}