如何解决如何从一个API Springboot Mongo更新和插入
我想使用此api更新并插入邮递员,如果您将ID与数据一起添加,则相同的数据将得到更新,没有ID意味着添加了新数据 不知道是否有可能,但对我来说很有意义:)
@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);
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。