错误写法:
@RequestMapping(value = "/department/updateByExampleSelective",method = RequestMethod.PUT)
int updateByExampleSelective(@RequestBody final Department record, @RequestBody final DepartmentExample example);
上面的代码是我卸载feign的service中的错误写法
这时候系统启动会报错:
Caused by: java.lang.IllegalStateException: Method has too many Body parameters:
因为一个request中只包含一个request body. 理解了这个,就会明白Spring MVC不支持多个@RequestBody。
解决办法:
1、使用@RequestParam代替@RequestBody,在某些地方是可以实现的,具体还得看情况
2、将接收参数定义为Map<String, Object>,然后使用map转object工具,转换成需要的对象。
此时,即使自定义的Param类中的属性即使比json数据中的属性少了,也没关系。
其中JSONUtils为自定义的工具类,可使用常见的fastjson等工具包包装实现。
@Controller public class TestController{ @RequestMapping("\test") @ResponseBody public Object test(@RequestBody Map<String, Object> models){ User user=JsonXMLUtils.map2object((Map<String, Object>)models.get("user"),User.class);
Address address=JsonXMLUtils.map2object((Map<String, Object>)models.get("address"),Address.class);
return models;
}
}
import com.alibaba.fastjson.JSON;
public class JsonXMLUtils { public static String obj2json(Object obj) throws Exception { return JSON.toJSONString(obj); } public static <T> T json2obj(String jsonStr, Class<T> clazz) throws Exception { return JSON.parseObject(jsonStr, clazz); } public static <T> Map<String, Object> json2map(String jsonStr) throws Exception { return JSON.parseObject(jsonStr, Map.class); } public static <T> T map2obj(Map<?, ?> map, Class<T> clazz) throws Exception { return JSON.parseObject(JSON.toJSONString(map), clazz); } }
参考:https://www.cnblogs.com/mahuan2/p/6008832.html