-1

Having trouble creating only one model ,created a photo model and payee model but want to add photo model to payee model but wont accept the binary part of photo . Keep getting binary error when trying to post off of postman ,aware that need to change controller but a bit stuck ,any links or advise for a begginner would be gratefull

//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;
            

//Payee constructor
                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;
                }
            
// Getters and Setters
                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;
            
//Photo constructor
                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();
                }
            }
            

Photo and payee PostMapping should be in one ,dont know where to go with this part //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
ChrisJnr Potgieter
  • 141
  • 1
  • 2
  • 13
  • looks like duplicate of this : https://stackoverflow.com/questions/63026184/like-to-post-an-image-with-binary-using-springboot-to-mongo-manage-to-do-it-wit – TmSmth Jul 22 '20 at 07:44
  • Have a look into this https://stackoverflow.com/questions/61343477/how-to-send-multipart-and-requestbody-together-in-spring-boot-2-x . When you send a file, you use `@RequestPart` to get it as shown in the image. You can't use `@RequestPart` and `@Request body` at the same time, to use `@RequestPart` for body too. – varman Jul 22 '20 at 07:53
  • Hi i keep getting a octet-stream error any thought – ChrisJnr Potgieter Jul 22 '20 at 08:16

1 Answers1

0

Figured it out in you model have image as binary and in youre controller have some like

@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); }

ChrisJnr Potgieter
  • 141
  • 1
  • 2
  • 13