使用一个请求调用插入和更新

Insert and update usining one request call

我想用这个api

更新和插入
@RequestMapping(value = "/updateBank", method = RequestMethod.POST, consumes = "multipart/form-data")
    public ResponseEntity<Bank> updateBank(@RequestPart("bank") @Valid Bank bank, @RequestPart("file") @Valid MultipartFile image) throws IOException
    {
        // routine to update a payee including image
        if (image != null)
            bank.setImage(new Binary(BsonBinarySubType.BINARY, image.getBytes()));
        Bank result = bankRepository.save(bank);
        return ResponseEntity.ok().body(result);
    }

你必须使用 Optional

 @RequestMapping(value = "/updateBank", method = RequestMethod.POST,  consumes = "multipart/form-data")
    public ResponseEntity<Bank> updateBank(@RequestPart("bank") @Valid Bank bank, @RequestPart("file") @Valid Optional<MultipartFile> image) throws IOException
    {
        // routine to update a payee including image
        image.ifPresent(pic ->
                {
                    try {
                        bank.setImage(new Binary(BsonBinarySubType.BINARY, pic.getBytes()));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                });


        Bank result = bankRepository.save(bank);
            return ResponseEntity.ok().body(result);


    }