前端传对象数组到后台List进行类型转换

很多情况下,后端需要从前端接受数据进行处理,这里写到几种接收数据的方法:

/**
 * 根据多个id获取之前上传的数据,这里接受List需要使用RequestParam注解,否则会报异常
 * 如果传递的是对象,则需要使用@RequestBody List bean,来进行接收
 */
@ApiOperation("根据id获取之前上传的数据,需要传ids")
@PostMapping("/getBeanById")
public Result getFileById(@RequestParam(value ="ids") List ids, HttpServletRequest request, HttpServletResponse response){
    Map map = Maps.newHashMap();
    for(String id : ids){
        String url = fileGetService.openlist(id);
        map.put(id, url);
    }
    return Result.success("获取数据成功", map);
}



/**
 * 保存
 */
@ApiOperation("保存信息")
@ApiImplicitParam(name = "teams", value = "jsonArray('格式':'JSONArray')",  dataType = "String")
@PostMapping("/add")
public Result add(@RequestParam("teams") String teams){
    // 第一种方式
    JSONArray jsonArray= JSON.parseArray(teams);
    List str = jsonArray.toJavaList(Team.class);
    
    // 第二种方式
    List  str = JSON.parseArray(teams, Team.class);
    
    // 处理其他业务......
}



// mapper.xml可以这样写

    insert into team
    
        create_by,
        del_flag,
        remarks
    
    values
    
        (
        #{item.createBy},
        #{item.delFlag},
        #{item.remarks}
        )
    



 

你可能感兴趣的:(Java)