主要是为了存档,碰到表单传对象数组的情况,一般都是一个表单只能传一个对象,后面经过跟前端的研究和讨论发现居然可以传对象数组,以此作为记录分享。

@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;//定位或关键字
    private float posX;
    private float posY;
    private String signOnPage;
    private Long sealId;
    private String key;
    private float width;
    private Integer signType;//2.骑缝章  1.其他

测试直接使用下标方式请求

直接使用属性下标的方式传递

示例代码:

@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);

java.lang.IllegalStateException: No primary or single unique constructor found for interface java.util.List

结果明显不适配报错

测试二使用对象包裹的方式传输

    @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);

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"'

头一次发现还可以使用这种方式,就像json传输一样,不过需要手动设置下标,对了,文件也可以这样传输,可以放对象里面也可以放外面,但是属性名字一样会双重注入。

SpringBoot的接收

1. 使用@RequestParam注解来接收表单数据中的数组对象。

以下是一个示例:

@PostMapping("/example")
public ResponseEntity<String> handleFormData(@RequestParam("objects") List<Object> objects) {
    // 处理接收到的对象数组
    return ResponseEntity.ok("Received " + objects.size() + " objects");

在上面的示例中,我们使用@RequestParam注解来声明我们要接收名为objects的表单参数,并将其映射到一个List<Object>类型的变量中。

2. 如果你的对象是一个自定义类,您可以使用@ModelAttribute注解来将表单数据映射到该类的实例中。以下是一个示例:

@PostMapping("/example")
public ResponseEntity<String> handleFormData(@ModelAttribute("customObject") CustomObject[] customObjects) {
    // 处理接收到的自定义对象数组
    return ResponseEntity.ok("Received " + customObjects.length + " custom objects");

在上面的示例中,我们使用@ModelAttribute注解来声明我们要接收名为customObject的表单参数,并将其映射到一个CustomObject[]类型的变量中。

3. `x-www-form-urlencoded` 和 `form-data` 协议的区别

`x-www-form-urlencoded` 和 `form-data` 是 HTTP 请求中常用的两种表单数据编码方式。

`x-www-form-urlencoded` 是默认的编码方式,它会将表单数据转换为键值对,并使用 `&` 符号进行分隔,然后将键值对以 `key1=value1&key2=value2` 的形式进行编码。这种编码方式通常用于较小的表单数据,如登录表单等。

而 `form-data` 则是一种更加灵活的编码方式,它可以处理二进制数据(如图片、文件等)以及文本数据。它会将每个表单字段封装成一个独立的部分,每个部分都可以设置自己的 Content-Type,这样就可以支持发送多个文件或者多个键值对。这种编码方式通常用于上传文件等操作。

传输数组队列不需要使用注解,在测试传输中不写注解反而能通过写了,写了@RequestPart注解反而通过不了,具体细节之后更新细则讲解,这篇主要讲解协议和请求,主要解决了表单形式传输对象的问题。

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

使用formData向后台传递数组对象

文章目录# 前端## form 上传## ajax 上传# 后端## MultipartFile## MultipartHttpServletRequest# 坑点:transferTo(File file)# 另外 ## form 上传 <form action="/demo/upload" method="POST" enctype="multipart/form-data"&...
最近重写个项目遇到个比较棘手的问题,老项目是 PHP 接口,这个接口同时兼容 POST json 和 form 表单,更骚的是连 form-data 也兼容。。。因为写 PHP 请求的对接方代码不严谨。详见这里。 而在 Java 中,一个接口只支持一种content-type,json 就用 @RequestBody,form 表单就用 @RequestP... form表单的常用形式如下: <form action="http://192.168.43.158:8082/uploadImage" method="post" enctype="multipart/form-data"> <input type="file" na...
文章目录一、错误经过最初的错误代码前端代码控制器代码错误的请求信息参数多次尝试二、分析后端接受参数的三种方式Get请求和Post请求的区别@Requestbody和@RequestParam的区别@RequestParam写与不写的区别三、解决示例代码方式一:使用@RequestParam方式二:使用@RequestBody四、总结 一、错误经过 写项目时,需要接收前端传过来的数组,于是用了往常接收字符串,数字等简单类型的方式接收,代码运行后报错。 最初的错误代码 var itemList =
@RequestMapping("/upload") @ResponseBody public String handleFileUpload(@RequestParam("file") MultipartFile file) { if (!file.is... type: "typeA", title: "titleA", authors: [{name:"upxuan", age:"18"}, {name:"susen", age:"18"}] console.log(Params) this.$ajax({ url: '/api/manualAdd', method: 'post', contentType: "application/json; charset=utf-8", dataT 2, 后台接收方式如下: @RequestMapping(value = "/publish/{eventId}", method = { RequestMethod.POST}) public ResponseResult toPublish(HttpServletRequest request, @PathVariable final 书书书书: 老师您好,请问意图识别的工作流刚开始测试能够成功,但是后面多测试几次,他就会报错:{ "$error": "The input data is incorrect as fields cannot be extracted from null values. Please check your input for any empty values." }这是怎么回事呢?