spring boot 请求中包含数组参数
一开始接口定义成这样
public ApiResult creMission(@RequestBody Mission mission,@RequestParam Integer[] copyIDs)
请求参数是这样
{ "missionTitle": "新增任务测试", "missionContent": "任务内容", "missionType": "1", "etc": "2018-10-01 18:30:00", "missionScore": "5", "executerID": "3", "deptID": "2", "mangerID": "2", "remake": "", "explain": "分值是5分,抄送刘光宗、李四", "copyIDs": [ 1, 11 ] }
结果就这样了……
Required Integer[] parameter 'copyIDs' is not present 15:31:05.374 [http-nio-8181-exec-4] WARN org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver - Resolved exception caused by Handler execution: org.springframework.web.bind.MissingServletRequestParameterException: Required Integer[] parameter 'copyIDs' is not present
改进方法一:
请求报文不变,改接口,重新定义一个实体类,把数组参数作为它的一个属性
新定义一个MissionParam
类,包含Mission
的全部属性,外加copyIDs
属性
接口定义成这样
public ApiResult creMission(@RequestBody MissionParam missionParam)
改进方法二:
接口需要改,重新定义一个实体类MissionParam
,里面只包含两个属性,Mission
和copyIDs
接口定义成:
public ApiResult creMission(@RequestBody MissionParam missionParam)
请求参数格式改一下
{ "mission": { "missionTitle": "新增任务测试", "missionContent": "任务内容", "missionType": "1", "etc": "2018-10-01 18:30:00", "missionScore": "5", "executerID": "3", "deptID": "2", "mangerID": "2", "remake": "", "explain": "分值是5分,抄送刘光宗、李四" }, "copyIDs": [ 4, 11 ] }
也可