springboot 将异步请求中的 JSON集合参数 接受时直接转换为java 集合对象

1 篇文章 0 订阅

SpringBoot异步请求JSON集合接受时转换为JAVA集合

异步请求

将 Array 转换为json字符串 : JSON.stringify(bugTypeArray);
未转换字符串会如下报错:

org.springframework.http.converter.HttpMessageNotReadableException: 
JSON parse error: Unrecognized token 'bugTypeArray': was expecting ('true', 'false' or 'null'); 
nested exception is com.fasterxml.jackson.core.JsonParseException: 
Unrecognized token 'bugTypeArray': was expecting ('true', 'false' or 'null')

使用json传输数据: contentType: “application/json;charset=UTF-8”
未设置请求类型会如下报错:Content type ‘application/x-www-form-urlencoded;charset=UTF-8’ not supported
在这里插入图片描述

var bugTypeArray = new Array();
 //获取选中的数据
 $("input[name='btSelectItem']:checked").each(function(){
  var bugType = $(this).parent().next().next().text();
  bugTypeArray.push(bugType)
 });
 
 //异步请求
 $.ajax({
  type:"post",
  url:"countNumber",
  data:JSON.stringify(bugTypeArray), //将数据转json字符串
  contentType: "application/json;charset=UTF-8", //json格式传输数据
  dataType:"json",
  success:function(data){
   $(".bootstrap-table button[name='refresh']").click();
   swal("更新成功!");
  },
  error:function(){
   swal("处理失败!");
  }
 });

处理接受的参数

使用 @RequestBody注解 将json字符串转换为java集合

 @ResponseBody
 public Object countNumber(@RequestBody List<String> bugTypeArray) {
  logger.info("size === " + bugTypeArray.size());
  return bugService.updateCountNumber(bugTypeArray);
 };

运行结果

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值