亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用不同密鑰的多個多部分休息呼叫

使用不同密鑰的多個多部分休息呼叫

瀟湘沐 2023-06-08 17:11:54
我正在嘗試使用in調用POST服務multipart form-data@RequestPartSpringBoot下面是可以與多個一起正常工作的代碼@RequestPart@PostMapping(value = "/saveDetails", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)public void saveDetails(@RequestPart("profileImage") MultipartFile profileImage,@RequestPart("addressImage") MultipartFile addressImage, @RequestPart("requestData") String requestData) {    try {        if(CommonUtils.isObjectNullOrEmpty(requestData) || CommonUtils.isObjectNullOrEmpty(profileImage) || CommonUtils.isObjectNullOrEmpty(addressImage)){            logger.warn("Data Should not be null ==>");        }        detailSaveService.saveOrUpdateDetails(profileImage, addressImage,requestData);    } catch (Exception e) {        logger.error("Error while saving profile Details ==>", e);    }}我需要改進我的代碼@RequestPart需要一個參數來使用不同的鍵(profileImage和addressImage)上傳多個文件。有什么建議嗎?請幫忙。
查看完整描述

1 回答

?
不負相思意

TA貢獻1777條經驗 獲得超10個贊

我有適合您的解決方案,請檢查@ModelAttribute請Spring-boot檢查以下示例

使用 getter setter 創建類并為所有不同的關鍵參數添加數據成員,請參考下面的示例

class FileUploadRequest? {

? ?private MultipartFile profileImage;

? ?private MultipartFile addressImage;

? ?private MultipartFile[] images; // you can use list or array

? ?private String requestData; // you can use another pojo or Jsonobject


? ?// add getter setter here...

}

在您的休息控制器中使用@ModelAttribute 并使用 MULTIPART_FORM_DATA_VALUE使用您的 HTTP 發布請求


@PostMapping(value = "/saveDetails", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)

public void saveDetails(@ModelAttribute FileUploadRequest uploadRequest) {

? try {

? ? ? if(CommonUtils.isObjectNullOrEmpty(uploadRequest.getRequestData()) || CommonUtils.isObjectNullOrEmpty(uploadRequest.getProfileImage()) || CommonUtils.isObjectNullOrEmpty(uploadRequest.getAddressImage())){

? ? ? ? ? logger.warn("Data Should not be null ==>");

? ? ? } else {

? ? ? ? ? detailSaveService.saveOrUpdateDetails(uploadRequest.getProfileImage()), uploadRequest.getAddressImage()),uploadRequest.getRequestData()));

? ? ? }


? ?} catch (Exception e) {

? ? ? ?logger.error("Error while saving profile Details ==>", e);

? ?}

}? ??

你可以參考這個鏈接示例@ModelAttributewith spring-bootwithangular


希望對你有用


查看完整回答
反對 回復 2023-06-08
  • 1 回答
  • 0 關注
  • 186 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號