2021-java与ant design pro,request.js同时传多个对象,以及解析封装到具体的实体类

8 篇文章 0 订阅
3 篇文章 0 订阅

一,导入jsonjar包

 <dependency>
         <groupId>com.alibaba</groupId>
         <artifactId>fastjson</artifactId>
         <version>1.2.56</version>
  </dependency>

二,前台代码

1,首先保证你的params是以下格式,控制台输出如下:

在这里插入图片描述
我这里是一个对象+4个对象数组

2,实际请求的前台代码

export async function saveCostEstimateApportionForm(body:{list: any},options?: { [key: string]: any }) {
  return request(`http://localhost:8006/costEa/saveCostApplyFor2`, {
    method: "POST",
    headers: {
      'Content-Type': 'application/json',
    },
    data:body , 
    // params: body,
    ...(options || {}),
    paramsSerializer: (body) => stringify(body) 
  }).then(function(response) { console.log(response); }) .catch(function(error) { console.log(error); }); 
}

三,后台接口

通过mapj接收数据,并解析

	@PostMapping("saveCostApplyFor2")
    @ApiOperation(value = "保存费用预估/分摊单", notes = "")
    public JsonResult saveCostApplyFo2(@RequestBody Map<String,Object> params) {
		//   获取前台传的单个对象
        Object obj = params.get("epaEstimateApportionMain");
        ObjectMapper objectMapper = new ObjectMapper();
        //   解析单个对象  
        EpaEstimateApportionMain epaEstimateApportionMain = objectMapper.convertValue(obj, EpaEstimateApportionMain.class);
		//   获取前台传的对象数组
        Object estimateApportionAttach1s = params.get("estimateApportionAttach1s");
        // 先将对象转换为json格式的string
        String jsonString = JSONArray.toJSONString(estimateApportionAttach1s);
        //  转换为JSONArray 
        JSONArray jsonArray= JSONArray.parseArray(jsonString);
		// 通过JSONArray的toJavaList方法转换为对象list
        List<EpaEstimateApportionAttach1> estimateApportionAttach1List = jsonArray.toJavaList(EpaEstimateApportionAttach1.class);
        //输出  ,验证是否接收到数据
        for (EpaEstimateApportionAttach1 epaEstimateApportionAttach1 : estimateApportionAttach1List) {
            System.out.println(epaEstimateApportionAttach1.toString());
        }
        return  null;
    }

如果有更好的方法,请大家留言指正,谢谢!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值