首先出现这种情况是因为有下面这种需求
-
$.ajax({
-
type:
"POST",
-
url:
"${pageContext.request.contextPath}/courses",
-
data: JSON.stringify({
-
course:course,
-
courseInfoList:courseInfoList
-
}),//将对象序列化成JSON字符串
-
-
dataType:
"json",
-
contentType :
'application/json;charset=utf-8',
//设置请求头信息
-
success:
function(data){
-
},
-
error:
function(res){
-
}
-
});
也就是在ajax传输数据时有多种数据类型在data域中
从而就会有下面这种controller
-
@RequestMapping(method = RequestMethod.POST ,consumes =
"application/json")
-
public String createCourse(@RequestBody Course course, @RequestBody List<CourseInfo> courseInfoList)
-
{
-
System.out.println(coursePackage.getCourse());
-
System.out.println(coursePackage.getCourseInfoList());
-
-
return
"/createCourse";
-
}
这样就会出现400错误,服务器无法理解这个请求
原因:
@requestbody的含义是在当前对象获取整个http请求的body里面的所有数据,因此spring就不可能将这个数据强制包装成Course或者List类型,并且从@requestbody设计上来说,只获取一次就可以拿到请求body里面的所有数据,就没必要出现有多个@requestbody出现在controller的函数的形参列表当中
如果想解决这种问题:
1.新建一个包装上面两种entity的entity类:
-
package com.yyc.entity;
-
-
import java.util.List;
-
-
public
class CoursePackage {
-
-
public CoursePackage() {
-
// TODO Auto-generated constructor stub
-
}
-
-
private Course course;
-
-
private List<CourseInfo> courseInfoList;
-
-
public void setCourse(Course course)
-
{
-
this.course = course;
-
}
-
-
public void setCourseInfoList(List<CourseInfo> courseInfoList)
-
{
-
this.courseInfoList = courseInfoList;
-
}
-
-
public Course getCourse()
-
{
-
return course;
-
}
-
-
public List<CourseInfo> getCourseInfoList()
-
{
-
return courseInfoList;
-
}
-
-
}
然后将controller函数改为
-
@RequestMapping(method = RequestMethod.POST ,consumes =
"application/json")
-
public String createCourse(@RequestBody CoursePackage coursePackage,Model model)
-
{
-
System.out.println(coursePackage.getCourse());
-
System.out.println(coursePackage.getCourseInfoList());
-
return
"/createCourse";
-
}
但是这样又显得比较不够简洁
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="{"mod":"popu_824"}"><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="{"mod":"popu_379"}" 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="{"mod":"popu_379"}">关注</a> </div> </div> </div>