RequestPart注解实现同时上传json 和 文件

最近要改造一个接口,原本是json传参,现在要加个文件,emm

RequestPart 可以帮我们做到,不同的key 以不同的content type解析

后端代码

@PostMapping("test")
    @ResponseBody
    public String test(@RequestPart("customer") Customer customer, @RequestPart MultipartFile file){
        System.out.println(file.getOriginalFilename());
        System.out.println(customer);
        return "test success";
    }

前端代码

$("#commit").click(function(){
    var fd = new FormData();


        //  第一种写法
        // fd.append("customerId", 12);
        // fd.append("customerName", "zhang");
        // fd.append("file", $('#f')[0].files[0]);
    

        //  第二种写法
        var customer = JSON.stringify({
            "customerId": 12,
            "customerName": "zhang"
        });
        //这里包装 可以直接转换成对象
        fd.append('customer', new Blob([customer], {type: "application/json"}));
        fd.append("file", $('#f')[0].files[0]);



        $.ajax({
            url: 'http://localhost:8080/test',
            type: 'post',
            processData: false,
            contentType: false,
            data: fd,
            success: function () {
                alert("ok")
            }
        })
  });

postman
默认没有这一列,需要点三个点 点出来
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值