记录后台接收数组
如果所有参数(包括数组)全部用一个实体类来接收,则不需要考虑前端数组的传参格式,他都能解析并封装到实体类里面。
这里的传参格式指:
后台用一个数组a来接收:
Integer[] a;
1
前端传值是下面两种的其中一种,都是可以的:
a=1&a=2&a=3 或者 a[0]=1&a[1]=2&a[3]=3
但是,如果直接在controller用数组对象接收的数组。而不是使用实体类来接收数组。那么 需要考虑以下两种情况。
1.前端使用ajax传的数组,并且格式是这样的:
http://192.168.0.143:8091/tourist/goods/a_goods?specValues[]=1&specValues[]=3&specImgs[]=1&specNames[]=1&specNames[]=3
1
那么,你需要@RequestParam(value = “specValues[]”)。
Controller: (参数前面加上注解 @RequestParam(value = "specValues[]") String[] specValues")
/**
* 添加商品
*/
@ApiOperation(value = "新增商品", notes = "卖家新增商品")
@ApiImplicitParams({
@ApiImplicitParam(name = "userId", value = "卖家用户ID", required = false, dataType = "int", paramType = "query"),
@ApiImplicitParam(name = "access_token", value = "access_token", required = false, dataType = "String", paramType = "query")
})
@RequestMapping(value = "a_goods", method = RequestMethod.POST)
public HttpEntity addGoods(FlkGoods goods, Integer userId, @RequestParam(value = "specValues[]") String[] specValues, @RequestParam(value = "specImgs[]") String[] specImgs, @RequestParam(value = "specNames[]") String[] specNames, HttpServletRequest request){
if (specNames == null || specImgs == null || specValues == null) {
logger.info("商品规格相关数据未传入");
throw new BusiException(ErrorKey.INVALID_REQUEST, "请完善商品规格!");
}
Map
return new ResponseEntity<>(returnMap, HttpStatus.OK);
}
前端传的参数是这样的:
2.前端使用ajax传的数组,表单序列化,并且格式是这样的:
Controller: (参数前面去掉@RequestParam注解)
/**
* 添加商品
*/
@ApiOperation(value = "新增商品", notes = "卖家新增商品")
@ApiImplicitParams({
@ApiImplicitParam(name = "userId", value = "卖家用户ID", required = false, dataType = "int", paramType = "query"),
@ApiImplicitParam(name = "access_token", value = "access_token", required = false, dataType = "String", paramType = "query")
})
@RequestMapping(value = "a_goods", method = RequestMethod.POST)
public HttpEntity addGoods(FlkGoods goods, Integer userId, String[] specValues, String[] specImgs, String[] specNames, HttpServletRequest request){
if (specNames == null || specImgs == null || specValues == null) {
logger.info("商品规格相关数据未传入");
throw new BusiException(ErrorKey.INVALID_REQUEST, "请完善商品规格!");
}
Map
return new ResponseEntity<>(returnMap, HttpStatus.OK);
}
http://localhost:8091/tourist/goods/a_goods?goodsName=asfasdf&specValues=11ml%2C12ml%2C13ml&specValues=%E5%A4%A7%2C%E5%B0%8F&specImgs=2.jpg&specNames=%E9%A2%9C%E8%89%B2&specNames=%E5%B0%BA%E7%A0%81
1
最后测试发现,使用方式1,通过swagger发送的参数,可以接收到。但是前端用post方式传过来,还是接收不到。查资料查了半天,仍然不知道原因,有知道的朋友,烦请告知我一下。。。万分感激!
结论
最后,我都用的实体类接收数据。
spring boot 接收参数,如果参数多,且包含了数组,建议直接用一个实体类接收。
RequestGoodsH5.java:
@Data
public class RequestGoodsH5 extends FlkGoods{
private Integer userId;
@ApiModelProperty(value = "规格值名称组合")
private String[] specValues;
@ApiModelProperty(value = "规格图片组合")
private String[] specImgs;
@ApiModelProperty(value = "规格名称组合")
private String[] specNames;
/**
* 其他更多参数
*/
}
controller:
/**
* 添加商品
*/
@ApiOperation(value = "新增商品", notes = "卖家新增商品")
@ApiImplicitParams({
@ApiImplicitParam(name = "userId", value = "卖家用户ID", required = false, dataType = "int", paramType = "query"),
@ApiImplicitParam(name = "access_token", value = "access_token", required = false, dataType = "String", paramType = "query")
})
@RequestMapping(value = "a_goods", method = RequestMethod.POST)
public HttpEntity addGoods(RequestGoodsH5 goods){
if (goods.getSpecValues() == null || goods.getSpecImgs() == null || goods.getSpecNames() == null) {
logger.info("商品规格相关数据未传入");
throw new BusiException(ErrorKey.INVALID_REQUEST, "请完善商品规格!");
}
FlkGoods goodsReal = new FlkGoods();
BeanUtil.copyPropertiesIgnoreNullValue(goods, goodsReal);
Map
return new ResponseEntity<>(returnMap, HttpStatus.OK);
}
通过一个实体类来接收,前端数组传这两种形式(a=1&a=2&a=3 或者 a[0]=1&a[1]=2&a[3]=3),spring boot 都可以把前端传过来的值封装到对应的参数里面。