Search code examples
javaspringspring-bootmultipartmultipartfile

Trying to save a file in springboot as an optional


I am trying to save a file as an optional in springboot with extra data. So the user should have the option to add an image or not to add an image. I recieve an no value error when there is no image. Everything saves fine when there is an image.

@RequestMapping(value = "/updateCustomer", method = RequestMethod.POST, consumes = "multipart/form-data")
    public ResponseEntity<?> updateCustomer(@RequestPart("customer") @Valid Customer customer, @RequestPart("file") @Valid Optional<MultipartFile> image) throws IOException {

        byte[] imageData = null;
        if (image.isPresent() && image.get() != null)
            imageData = image.get().getBytes();
        if (imageData == null && customer.getId() != null) {
            Optional<Customer> readCustomer = customerRepository.findById(customer.getId());
            if (readCustomer.get() != null)
                imageData = readCustomer.get().getImage().getData();
        }
        if (imageData != null) {
            customer.setImage(new Binary(BsonBinarySubType.BINARY, imageData));
        }



        Customer result = customerRepository.save(customer);
        return ResponseEntity.ok().body(result);
    }

Model used ing controller

public class Customer {

    @Id
    private String id;
    private String username;
    private String name;
    private String surname;
    private String dob;
    private String position;
    private String email;
    private String contactNo;
    private String status;
    private Integer notificationValue;
    private Address address;
    private BusinessInformation businessInformation;
    private Binary image;
    private List<UserRolls> userRolls;
    private List<CustomerITMModules> entityITMModules;

Error I'm getting

java.lang.NullPointerException: Cannot invoke "org.bson.types.Binary.getData()" because the return value of "com.mqa.modules.Admin.mst_Entity.models.Customer.getImage()" is null

Solution

  • So took everyone's advice. Made a change to the controller for optional image. Not the most efficient but it works

     @RequestMapping(value = "/updateCustomer", method = RequestMethod.POST, consumes = "multipart/form-data")
        public ResponseEntity<?> updateCustomer(@RequestPart("customer") @Valid Customer customer,
                                                @RequestParam(name="file", required=false) MultipartFile image) throws IOException {
    
    //Create New User
    if(customer.getId() == ""){
        System.out.println("New User");
    }else if(customer != null  ){  //On Edit User
        Customer user = this.customerRepository.findUsersByEmail(customer.getEmail());
        if(image == null && user.getImage() != null){  // If User has image save same image
            byte[] existingImage = user.getImage().getData();
            customer.setImage(new Binary(BsonBinarySubType.BINARY, existingImage));
            System.out.println(customer.getName());
        }
    
    }
            //Save New Image
            if(customer.getId() != null  && image!= null){
                System.out.println("TestNew");
                byte[] newImage = image.getBytes();
                customer.setImage(new Binary(BsonBinarySubType.BINARY, newImage));
            }
    
    
            
    
    
            Customer result = customerRepository.save(customer);
            return ResponseEntity.ok().body(result);
        }