controller层利用JSON接收对象数组

某些场景下,我们需要接收自定义对象的List/数组,这时候使用form表单格式传送可能不方便

可以使用JSON的格式

首先 后端:

//com.alibaba.fastjson;

@PostMapping("/submitApply")
//@RequestParam Long settlementApplyId, @RequestBody ArrayList<UploadFileQuery> attachments, @RequestParam String note
public ResponseBean submitApply(@RequestBody JSONObject json) {
    Long settlementApplyId= json.getLong("settlementApplyId");
    String note = json.getString("note");
    List<UploadFileQuery> attachments = json.getJSONArray("attachments").toJavaList(UploadFileQuery.class);
    return null;
}

postman 测试:

 

附:

UploadFileQuery
@Data
@Accessors(chain = true)
public class UploadFileQuery implements Serializable {

    /**
     * 源文件名
     */
    private String name;

    /**
     * 上传到阿里云后文件名
     */
    private String ossName;

    /**
     * 带签名阿里云地址
     */
    private String url;
}
发布了328 篇原创文章 · 获赞 23 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/lbh199466/article/details/103770935