2 sinat 36057974 sinat_36057974 于 2016.09.06 11:11 提问

springMVC怎么接受传来的对象的集合?

@RequestMapping(value = "insertDetails")
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
public void insertDetails(
@RequestBody List lisRecInfoDetailsObjs,
@RequestParam(required = true , value ="examinationId")Integer examinationId,
@RequestParam(required = true , value ="lisRecId")Integer lisRecId,
@RequestParam(required = false , value ="userId")Integer userId,
@RequestParam(required = true , value ="startTime")Date startTime,
@RequestParam(required = true , value ="endTime")Date endTime,
@RequestParam(required = true , value ="score")Integer score,
@RequestParam(required = true , value ="totalQuestion")Integer totalQuestion,
@RequestParam(required = true , value ="correctQuestion")Integer correctQuestion,
@RequestParam(required = true , value ="errorQuestion")Integer errorQuestion,
@RequestParam(required = true , value ="completeness")Integer completeness,
HttpServletRequest request, HttpServletResponse response) {}
这样写有问题吗

5个回答

of214
of214   2016.09.06 12:49

可以定义一个对象,然后加上@requestparam注解,或者传入一个字符串,在通过jackson或者fastjson之类的工具类转换成对应的对象,可以参考下这篇文章http://58coding.com/article/detail/24633146704155301

sinat_36057974
sinat_36057974 谢谢
大约一年之前 回复
donggua3694857
donggua3694857   Rxr 2016.09.06 12:55

加上@requestBody应该会直接解析的,或者你把那个json字符串传过来用JSON.parseArray(jsonstr,xxx.class)就可以转换成泛型集合了

donggua3694857
donggua3694857 回复sinat_36057974: 是的
大约一年之前 回复
sinat_36057974
sinat_36057974 现在这样就可以接收到数据了吗
大约一年之前 回复
sinat_36057974
sinat_36057974   2016.09.06 11:41

{
"lisRecInfoDetailsObjs":[
{"questionNumber":"试卷题数","iscorrect":"是否正确","userAnswer":"用户答案"},
{"questionNumber":"试卷题数","iscorrect":"是否正确","userAnswer":"用户答案"},
{"questionNumber":"试卷题数","iscorrect":"是否正确","userAnswer":"用户答案"},
......
],
"examinationId":"试卷ID",
"lisRecId":"答卷ID",
"userId":"答卷用户ID",
"startTime":"开始时间",
"endTime":"结束时间",
"score":"答题分数",
"totalQuestion":"试卷总题数",
"correctQuestion":"正确题数",
"errorQuestion":"错误题数",
"completeness":"完成度"
}
jason数据大概是这种格式

Eugene_wei
Eugene_wei   2016.09.06 11:42

加上@requestBody

sinat_36057974
sinat_36057974 加上@requestBody就没问题了吗?大神
大约一年之前 回复
fu415037685
fu415037685   2016.09.06 13:25

解决了没有,你是怎么解决这个问题的 ,我也遇到这个问题了

sinat_36057974
sinat_36057974 目前我还是这样写的,还没有测试
大约一年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片