【问题标题】:Insert and update usining one request call使用一次请求调用插入和更新
【发布时间】:2020-09-25 07:47:23
【问题描述】:

我想使用这个 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);
    }

【问题讨论】:

    标签: mongodb spring-boot api insert


    【解决方案1】:

    你必须使用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);
    
    
        }
    

    【讨论】:

    • 下一次,请确保以正确的格式发布代码。
    猜你喜欢
    • 2012-03-15
    • 1970-01-01
    • 2020-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多