spring boot 发送多层json参数的post请求

在向我搭建的后台发送请求的时候,由于json参数结构比较复杂一直没有发送成功。

我发送的json参数格式如下:

{
    "pre_prepare_in": {
        "trace":{
            "user_num": 10,
            "contect": [
                {"user":[1,2], "price_trade":12},
                {"user":[2,3], "price_trade":12},
                {"user":[3,4], "price_trade":12},
                {"user":[4,5], "price_trade":12},
                {"user":[5,6], "price_trade":12},
                {"user":[6,7], "price_trade":12},
                {"user":[7,8], "price_trade":12},
                {"user":[8,9], "price_trade":12},
                {"user":[9,0], "price_trade":12},
                {"user":[0,1], "price_trade":12}]
            },
        "user_proassets":{
            "user_num": 10,
            "content": [
                100,
                100,
                100,
                100,
                100,
                100,
                100,
                100,
                100,
                100 ]

            }
    },
    "block_num": 9,
    "pointname": 1
}

该参数只能够被读取到block_num及pointname,而pre_prepare_in却显示为空。

后来我分析了原因,该参数A我发送的时候将参数封装成A类,A类下有三个属性,对应pre_prepare_in的属性被我定义为JSONObject类。问题就在这里,JSONObject类在post发送过程中会因为无法正常接收而丢失内容(我的发送方法不行可能也有别的方法,各路大神勿喷)。大概因为JSONObject这个类结构不固定,接收时没办法匹配字段。

所以就要将自己要发送的json文件的每个字段都在类内封装好。如下看我的代码:

//参数类A
public class Pre_prepare {


    private Pre_prepare_in pre_prepare_in;
    private Integer block_num;
    private String pointname;
//  get and set
}

//参数类A的子类B
public class Pre_prepare_in {

    private RequestTrace trace;
    private RequestProass user_proassets;
//  get and set
}

//子类B的子类C
public class RequestTrace {

    private Integer user_num;
    private List<Trace_item> content;
//  get and set
}

//子类B的子类D
public class RequestProass {
    private Integer user_num;
    private int[] content;
//  get and set
}

//子类C的子类E
public class Trace_item {
    private int[] user;
    private Integer price_trade;
//  get and set
}

post函数:

 @SneakyThrows
    private void dopost_preprepare(Pre_prepare pre_prepare,URL url){
        
        HttpPost post = new HttpPost(url);
        CloseableHttpClient client = HttpClientBuilder.create().build();
        CloseableHttpResponse response = null;

    
        String pre_prepare_str = JSON.toJSONString(pre_prepare);
        StringEntity stringEntity = new StringEntity(pre_prepare_str,StandardCharsets.UTF_8);
        post.setEntity(stringEntity);
        post.setHeader("Content-Type","application/json;charset=utf8");

        try {
            response = client.execute(post);
            StatusLine statusLine = response.getStatusLine();
            System.out.println(statusLine.getStatusCode());
            org.apache.http.HttpEntity httpEntity = response.getEntity();
            System.out.println(EntityUtils.toString(httpEntity, StandardCharsets.UTF_8));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            try {
                response.close();
                client.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

        }
    }

接口:


@RestController
@RequestMapping("root")
public class threadController {

    @PostMapping("pre_prepare")
    public String getpre_prepare(@RequestBody Pre_prepare pre_prepare ){
        System.out.println("收到请求");
        return "内容" + pre_prepare; 
    }

过程当中需要注意的是字段要对应,用postman发送请求的时候也是一样,键值的名字要与类的属性名一样,否则也会出现接收不到内容为空的情况。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

烛火微亮

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值