springboot接口 接收参数为实体对象跟MultipartFile对象报错。

springboot接口 接收参数为实体对象跟文件对象报错。
方法参数为
A a, B b, @RequestParam MultipartFile frontImg,
@RequestParam MultipartFile backImg

如上是可以接受到2个文件到,但是A,B实体类接受不到

@Requestbody A a, @Requestbody B b, @RequestParam MultipartFile frontImg,
@RequestParam MultipartFile backImg
这样到话 报错信息如下

{
  "timestamp": 1532321861450,
  "status": 415,
  "error": "Unsupported Media Type",
  "exception": "org.springframework.web.HttpMediaTypeNotSupportedException",
  "message": "Content type 'multipart/form-data;boundary=----WebKitFormBoundarywczeDlb5Y86wuQSJ;charset=UTF-8' not supported",
  "path": "/personalCenter/saveUserRealInfo"
}

重金求人解决问题

你要把文件和普通数据类型分开接口传输,不可以兼容多个类型参数,
建议是传文件一个接口,返回url路径,再和普通数据一起提交,就是两次
企业上的做法都是这样,先用文件服务器保存文件,返回文件路径

这个属于HTTP基础,HTTP请求分为了消息头和消息体,头信息里面的Content-Type字段定义了消息体的请求格式,SpringMvc是按照请求头来解析消息内容的。一般上传文件是单独做成一个接口,上传成功后返回文件的地址。
你如果要传json数据,那么前端到后端的http请求头里为: "Content-Type: application/json",如果你想同时传A和B两个实体,那么你需要新建一个实体C

 public class C{
     private A a;
     private B b;
     //省略setter getter
 }
 //然后前端传json数据格式为
 {
     "a":{"name":"zhangsan"},
     "b":{"num":123456}
 }
 //然后用yourMethodName(@RequestBody C c)来接收数据

如果你要上传文件,那么HTTP头信息里面的Content-Type字段为:"Content-Type: multipart/form-data"。
现在你在一个接口里想要同时接收两种不同类型的消息,SpringMVC不知道该如何解析消息,自然就报错了。

应该是返回类型问题

    @ResponseBody 
        @RequestMapping(params="upload", method = RequestMethod.POST)
    public void upload(@RequestParam("file") MultipartFile file, Model model) throws IllegalStateException, IOException  {
        String url = this.req.getScheme()+"://"+ this.req.getServerName()+":"+ this.req.getServerPort()+ this.req.getServletPath()+ this.req.getSession().getServletContext().getRealPath("/pictures");
        if (file.isEmpty()) {
            System.out.println("上传文件不能为空");
        }
        System.out.println("文件路径"+ url);
        // 图片上传成功后,将图片的地址写到数据库
        String filePath = this.req.getSession().getServletContext().getRealPath("/pictures");
        // 上传文件原始名称
        String originFileName = file.getOriginalFilename();
        // 新的图片的名称
        String newFileName = UUID.randomUUID()+ originFileName.substring(originFileName.lastIndexOf("."));
        // 新文件
        File file1 = new File(filePath + File.separator+newFileName);
        // 将内存中的文件写入磁盘
        file.transferTo(file1);
        System.out.println("上传文件wancheng");
        setCommonData(model);
       //返回的json串
    }

用MultipartFile 数组进行接收, @RequestParam MultipartFile[] files,

前台在向后台提交表单时,如果包含文件,需要为form表单设置enctype="multipart/form-data"

返回415就说明参数格式不对。
先问你一句:你这个请求不带A a,B b参数,只有后两个参数能成功 么?
如果能成功,你就
你把你那四个参数放入到一个实体类中class UploadParam {
A a,
B b,
MultipartFile frontImg;
MultipartFile backImg;
},相当于你的controller那个方法中只有一个参数UploadParam