controller函数中参数列表使用多个@RequestBody

首先出现这种情况是因为有下面这种需求


   
   
  1. $.ajax({
  2. type: "POST",
  3. url: "${pageContext.request.contextPath}/courses",
  4. data: JSON.stringify({
  5. course:course,
  6. courseInfoList:courseInfoList
  7.        }),//将对象序列化成JSON字符串
  8. dataType: "json",
  9. contentType : 'application/json;charset=utf-8', //设置请求头信息
  10. success: function(data){
  11. },
  12. error: function(res){
  13. }
  14. });

也就是在ajax传输数据时有多种数据类型在data域中

从而就会有下面这种controller


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

这样就会出现400错误,服务器无法理解这个请求

原因:

@requestbody的含义是在当前对象获取整个http请求的body里面的所有数据,因此spring就不可能将这个数据强制包装成Course或者List类型,并且从@requestbody设计上来说,只获取一次就可以拿到请求body里面的所有数据,就没必要出现有多个@requestbody出现在controller的函数的形参列表当中

如果想解决这种问题

1.新建一个包装上面两种entity的entity类:


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

然后将controller函数改为


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

但是这样又显得比较不够简洁

2..用Map<String, Object>接受request body,自己反序列化到各个entity中。

例:下面这篇博客比较好:https://www.cnblogs.com/mahuan2/p/6008832.html

  •                     <li class="tool-item tool-active is-like "><a href="javascript:;"><svg class="icon" aria-hidden="true">
                            <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#csdnc-thumbsup"></use>
                        </svg><span class="name">点赞</span>
                        <span class="count">4</span>
                        </a></li>
                        <li class="tool-item tool-active is-collection "><a href="javascript:;" data-report-click="{&quot;mod&quot;:&quot;popu_824&quot;}"><svg class="icon" aria-hidden="true">
                            <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#icon-csdnc-Collection-G"></use>
                        </svg><span class="name">收藏</span></a></li>
                        <li class="tool-item tool-active is-share"><a href="javascript:;"><svg class="icon" aria-hidden="true">
                            <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#icon-csdnc-fenxiang"></use>
                        </svg>分享</a></li>
                        <!--打赏开始-->
                                                <!--打赏结束-->
                                                <li class="tool-item tool-more">
                            <a>
                            <svg t="1575545411852" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5717" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M179.176 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5718"></path><path d="M509.684 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5719"></path><path d="M846.175 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5720"></path></svg>
                            </a>
                            <ul class="more-box">
                                <li class="item"><a class="article-report">文章举报</a></li>
                            </ul>
                        </li>
                                            </ul>
                </div>
                            </div>
            <div class="person-messagebox">
                <div class="left-message"><a href="https://blog.csdn.net/qq_34608620">
                    <img src="https://profile.csdnimg.cn/8/6/F/3_qq_34608620" class="avatar_pic" username="qq_34608620">
                                            <img src="https://g.csdnimg.cn/static/user-reg-year/1x/4.png" class="user-years">
                                    </a></div>
                <div class="middle-message">
                                        <div class="title"><span class="tit"><a href="https://blog.csdn.net/qq_34608620" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}" target="_blank">YT___YYC</a></span>
                                            </div>
                    <div class="text"><span>发布了11 篇原创文章</span> · <span>获赞 7</span> · <span>访问量 3万+</span></div>
                </div>
                                <div class="right-message">
                                            <a href="https://im.csdn.net/im/main.html?userName=qq_34608620" target="_blank" class="btn btn-sm btn-red-hollow bt-button personal-letter">私信
                        </a>
                                                            <a class="btn btn-sm  bt-button personal-watch" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}">关注</a>
                                    </div>
                            </div>
                    </div>
    
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值