Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
216 views
in Technique[技术] by (71.8m points)

mongodb - Like to post an image with binary using springboot to mongo ,manage to do it with only the image but struggling to do it with more data

having trouble add both models to one ,manage to add photo alone but want to add everything as one model Like to post an image with binary using springboot to mongo ,manage to do it with only the image but struggling to do it with more data Like to post an image with binary using springboot to mongo ,manage to do it with only the image but struggling to do it with more data

 //payee model    
    public class Payee {
            @Id
            private String id;
        
            private Contact contact;
            private String notes;
            private String project;
            private String member;
            private Integer staffMember;
            private Photo photo;
            private BankAccount userBankAccount;
            private PayeeBankAccount payeeBankAccountList;
        
            public Payee(Contact contact, String notes, String project, String member, Integer staffMember,
                         Photo photo, BankAccount userBankAccount, PayeeBankAccount payeeBankAccountList) {
                this.contact = contact;
                this.notes = notes;
                this.project = project;
                this.member = member;
                this.staffMember = staffMember;
                this.photo = photo;
        
                this.userBankAccount = userBankAccount;
                this.payeeBankAccountList = payeeBankAccountList;
            }
        
            public String getId() {return id;}
        
            public void setId(String id) {this.id = id;}
        
            public Contact getContact() { return contact; }
        
            public void setContact(Contact contact) { this.contact = contact; }
        
            public String getNotes() { return notes; }
        
            public void setNotes(String notes) { this.notes = notes; }
        
            public String getProject() { return project; }
        
            public void setProject(String project) { this.project = project; }
        
            public String getMember() { return member; }
        
            public void setMember(String member) { this.member = member; }
        
            public Integer getStaffMember() { return staffMember; }
        
            public void setStaffMember(Integer staffMember) { this.staffMember = staffMember; }
        
            public Photo getPhoto() {return photo;}
        
            public void setPhoto(Photo photo) {this.photo = photo;}
        
            public BankAccount getUserBankAccount() { return userBankAccount; }
        
            public void setUserBankAccount(BankAccount userBankAccount) { this.userBankAccount = userBankAccount}
        
            public PayeeBankAccount getPayeeBankAccountList() { return payeeBankAccountList; }
        
            public void setPayeeBankAccountList(PayeeBankAccount payeeBankAccountList){this.payeeBankAccountList= payeeBankAccountList; }
        
        
        }
        //photo model
        public class Photo {
        
            @Id
            private String id;
        
            private String title;
        
            private Binary image;
        
            public Photo(String id, String title, Binary image) {
                this.id = id;
                this.title = title;
                this.image = image;
            }
        
            public Photo(String title) {
                super();
                this.title = title;
            }
        
            public String getId() {
                return id;
            }
        
            public void setId(String id) {
                this.id = id;
            }
        
            public String getTitle() {
                return title;
            }
        
            public void setTitle(String title) {
                this.title = title;
            }
        
            public Binary getImage() {
                return image;
            }
        
            public void setImage(Binary image) {
                this.image = image;
            }
        
            @Override
            public String toString() {
                return "Photo [id=" + id + ", title=" + title + ", image=" + image + "]";
            }
        
        }
        
    //photo service
        @Service
        public class PhotoService {
        
            @Autowired
            private PhotoRepository photoRepo;
        
            public Photo getPhoto(String id) {
                return photoRepo.findById(id).get();
            }
        
            public String addPhoto(String title, MultipartFile file) throws IOException {
                Photo photo = new Photo(title);
                photo.setImage(new Binary(BsonBinarySubType.BINARY, file.getBytes()));
                photo = photoRepo.insert(photo);
                return photo.getId();
            }
        }
        
        //add payee
            @PostMapping('/addPayee')
            public void insert(@RequestBody Payee payee){
                this.payeeRepository.save(payee);
            }
        
    //add photo
         @PostMapping('/photos/add')
            public String addPhoto(@RequestBody Payee payee , @RequestParam("title") String title, @RequestParam("image") MultipartFile image, Model model)
                    throws IOException {
                String id = photoService.addPhoto(title,image);
                return id;
            }][1]][1]
            


  [1]: https://i.stack.imgur.com/lF6KZ.png
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

figured it out, set image as binary in model

@RequestMapping(value = "/update", method = RequestMethod.POST, consumes = "multipart/form-data") public ResponseEntity update(@RequestPart("payee") @Valid Payee payee, @RequestPart("file") @Valid MultipartFile image) throws IOException { // routine to update a payee including image if (image != null) payee.setImage(new Binary(BsonBinarySubType.BINARY, image.getBytes())); Payee result = payeeRepository.save(payee); return ResponseEntity.ok().body(result); }


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...