前言
主要是為了存檔,碰到表單傳對(duì)象數(shù)組的情況,一般都是一個(gè)表單只能傳一個(gè)對(duì)象,后面經(jīng)過(guò)跟前端的研究和討論發(fā)現(xiàn)居然可以傳對(duì)象數(shù)組,以此作為記錄分享。
@Data public class SealLocationInfoRequest implements Serializable { private static final long serialVersionUID = 2392716281569231777L; private Long contractId; private Long serverId; private String filePath; private List<SealLocationInfo> sealLocationInfoList; } @Data public class SealLocationInfo implements Serializable { private static final long serialVersionUID = -8706741125508276806L; private Integer posType;//定位或關(guān)鍵字 private float posX; private float posY; private String signOnPage; private Long sealId; private String key; private float width; private Integer signType;//2.騎縫章 1.其他 }
測(cè)試直接使用下標(biāo)方式請(qǐng)求
直接使用屬性下標(biāo)的方式傳遞
請(qǐng)求:

示例代碼:
@PostMapping(value = "/upload/multiple") public ResponseEntity<ResponseResult<List<Object>>> uploadMultiple1213Batch( MultipartFile pdfFile, // @ModelAttribute("request") List<SealLocationInfoRequest> request) throws Exception { // @RequestParam("hosts") SealLocationInfoRequest hosts) throws Exception { // @ModelAttribute("hosts") SealLocationInfoRequest hosts) throws Exception { return OpsResponse.ok(null); }
結(jié)果:
java.lang.IllegalStateException: No primary or single unique constructor found for interface java.util.List
結(jié)果明顯不適配報(bào)錯(cuò)
測(cè)試二使用對(duì)象包裹的方式傳輸
@PostMapping(value = "/upload/multiple") public ResponseEntity<ResponseResult<List<Object>>> uploadMultiple1213Batch( MultipartFile pdfFile, // @ModelAttribute("request") // List<SealLocationInfoRequest> request) throws Exception { // @RequestParam("hosts") SealLocationInfoRequest hosts) throws Exception { SealLocationInfoRequest hosts) throws Exception { return OpsResponse.ok(null); }
請(qǐng)求體

curl的方式
curl --location 'http://localhost:8088/upload/record/upload/multiple' \ --header 'Content-Type: multipart/form-data' \ --header 'Accept: */*' \ --header 'Authorization: acf179d575a7492fbbf5deefbdc69fbd' \ --header 'from-service: trade-gateway' \ --header 'gateway_header: 2131321' \ --header 'traceId: 12312' \ --form 'sealLocationInfoList[0].posX="123213"'
頭一次發(fā)現(xiàn)還可以使用這種方式,就像json傳輸一樣,不過(guò)需要手動(dòng)設(shè)置下標(biāo),對(duì)了,文件也可以這樣傳輸,可以放對(duì)象里面也可以放外面,但是屬性名字一樣會(huì)雙重注入。
SpringBoot的接收
1. 使用@RequestParam注解來(lái)接收表單數(shù)據(jù)中的數(shù)組對(duì)象。
以下是一個(gè)示例:
@PostMapping("/example") public ResponseEntity<String> handleFormData(@RequestParam("objects") List<Object> objects) { // 處理接收到的對(duì)象數(shù)組 return ResponseEntity.ok("Received " + objects.size() + " objects"); }
在上面的示例中,我們使用@RequestParam
注解來(lái)聲明我們要接收名為objects
的表單參數(shù),并將其映射到一個(gè)List<Object>
類型的變量中。
2. 如果你的對(duì)象是一個(gè)自定義類,您可以使用@ModelAttribute注解來(lái)將表單數(shù)據(jù)映射到該類的實(shí)例中。以下是一個(gè)示例:
@PostMapping("/example") public ResponseEntity<String> handleFormData(@ModelAttribute("customObject") CustomObject[] customObjects) { // 處理接收到的自定義對(duì)象數(shù)組 return ResponseEntity.ok("Received " + customObjects.length + " custom objects"); }
在上面的示例中,我們使用@ModelAttribute
注解來(lái)聲明我們要接收名為customObject
的表單參數(shù),并將其映射到一個(gè)CustomObject[]
類型的變量中。
3. `x-www-form-urlencoded` 和 `form-data` 協(xié)議的區(qū)別
`x-www-form-urlencoded` 和 `form-data` 是 HTTP 請(qǐng)求中常用的兩種表單數(shù)據(jù)編碼方式。
`x-www-form-urlencoded` 是默認(rèn)的編碼方式,它會(huì)將表單數(shù)據(jù)轉(zhuǎn)換為鍵值對(duì),并使用 `&` 符號(hào)進(jìn)行分隔,然后將鍵值對(duì)以 `key1=value1&key2=value2` 的形式進(jìn)行編碼。這種編碼方式通常用于較小的表單數(shù)據(jù),如登錄表單等。
而 `form-data` 則是一種更加靈活的編碼方式,它可以處理二進(jìn)制數(shù)據(jù)(如圖片、文件等)以及文本數(shù)據(jù)。它會(huì)將每個(gè)表單字段封裝成一個(gè)獨(dú)立的部分,每個(gè)部分都可以設(shè)置自己的 Content-Type,這樣就可以支持發(fā)送多個(gè)文件或者多個(gè)鍵值對(duì)。這種編碼方式通常用于上傳文件等操作。
總結(jié)
傳輸數(shù)組隊(duì)列不需要使用注解,在測(cè)試傳輸中不寫(xiě)注解反而能通過(guò)寫(xiě)了,寫(xiě)了@RequestPart注解反而通過(guò)不了,具體細(xì)節(jié)之后更新細(xì)則講解,這篇主要講解協(xié)議和請(qǐng)求,主要解決了表單形式傳輸對(duì)象的問(wèn)題。
參考
Difference Between form-data, x-www-form-urlencoded and raw in Postman | Baeldung
Forms in HTML documents
javascript - appending array to FormData and send via AJAX - Stack Overflow文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-778000.html
使用formData向后臺(tái)傳遞數(shù)組對(duì)象文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-778000.html
到了這里,關(guān)于SpringBoot處理form-data表單接收對(duì)象數(shù)組的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!