controller函数中参数,无法使用多个@RequestBody(),解决方式

本文参考 https://blog.csdn.net/qq_34608620/article/details/80635139

  • @requestbody的含义是在当前对象获取整个http请求的body里面的所有数据,因此spring就不可能将这个数据强制包装成A参数和B参数,
  • 一般情况下,@RequestBody只处理 Content-Type 是 application/json 或者 application/xml。
  • 并且从@requestbody设计上来说,只获取一次就可以拿到请求body里面的所有数据,就没必要出现有多个@requestbody出现在controller的函数的形参列表当中 

解决方法:

  一、前端,data: JSON.stringify(excelRelated.chooseData), 直接传送json字符串

                $.ajax({
                    //这里是php的url格式
                    url: "http://127.0.0.1:8081/source/downloadExcel",
                    type: "post",
                    contentType : 'application/json; charset=UTF-8',
                    data: JSON.stringify(excelRelated.chooseData),
                    dataType: 'json',
                    success: function (data) {
                        alert(111)
                    }
                })

  二、后台

封装成实体类对象

 1 package com.yyc.entity;
 2  
 3 import java.util.List;
 4  
 5 public class CoursePackage {
 6  
 7     public CoursePackage() {
 8         // TODO Auto-generated constructor stub
 9     }
10     
11     private Course course;
12     
13     private List<CourseInfo> courseInfoList;
14     
15     public void setCourse(Course course)
16     {
17         this.course = course;
18     }
19     
20     public void setCourseInfoList(List<CourseInfo> courseInfoList)
21     {
22         this.courseInfoList = courseInfoList;
23     }
24     
25     public Course getCourse()
26     {
27         return course;
28     }
29     
30     public List<CourseInfo> getCourseInfoList()
31     {
32         return courseInfoList;
33     }
34  
35 }

controller对应代码

1 @RequestMapping(method = RequestMethod.POST ,consumes = "application/json")
2     public String createCourse(@RequestBody CoursePackage coursePackage)
3     {
4         System.out.println(coursePackage.getCourse());
5         System.out.println(coursePackage.getCourseInfoList());
6         return "/createCourse";
7     }

猜你喜欢

转载自www.cnblogs.com/zhangwensi/p/11797768.html