You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Java中按JSON payload大小实现最多30对象的动态API调用?

Java中拆分JSON并动态发起API调用的实现方案

1. 定义JSON对应的Java实体类

先创建与输入JSON结构匹配的实体类,方便后续解析和操作:

import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.List;

// 对应整个JSON payload
public class Payload {
    private int total;
    @JsonProperty("plist")
    private List<Item> itemList;

    // 构造方法(用于批次请求)
    public Payload(int total, List<Item> itemList) {
        this.total = total;
        this.itemList = itemList;
    }

    // 无参构造(Jackson解析需要)
    public Payload() {}

    // Getter和Setter方法
    public int getTotal() { return total; }
    public void setTotal(int total) { this.total = total; }
    public List<Item> getItemList() { return itemList; }
    public void setItemList(List<Item> itemList) { this.itemList = itemList; }
}

// 对应plist中的单个对象
public class Item {
    private String reference;
    private String date;
    private int value;

    // Getter和Setter方法
    public String getReference() { return reference; }
    public void setReference(String reference) { this.reference = reference; }
    public String getDate() { return date; }
    public void setDate(String date) { this.date = date; }
    public int getValue() { return value; }
    public void setValue(int value) { this.value = value; }
}

2. 解析原始JSON字符串

使用Jackson的ObjectMapper将输入的JSON字符串解析为Java对象:

import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonParser {
    private static final ObjectMapper objectMapper = new ObjectMapper();

    public static Payload parse(String jsonStr) throws Exception {
        return objectMapper.readValue(jsonStr, Payload.class);
    }
}

3. 拆分列表为指定大小的批次

实现工具方法,将大列表拆分为多个子列表,每个子列表最多包含30个元素:

import java.util.ArrayList;
import java.util.List;

public class BatchSplitter {
    public static <T> List<List<T>> split(List<T> source, int batchSize) {
        List<List<T>> batches = new ArrayList<>();
        int total = source.size();
        for (int i = 0; i < total; i += batchSize) {
            int end = Math.min(i + batchSize, total);
            batches.add(source.subList(i, end));
        }
        return batches;
    }
}

4. 动态发起API调用

遍历每个批次,将子列表包装为请求JSON,然后调用目标API:

import java.util.List;

public class DynamicApiCaller {
    private static final int MAX_BATCH_SIZE = 30;
    private static final ObjectMapper objectMapper = new ObjectMapper();

    public static void process(String inputJson) throws Exception {
        // 解析JSON
        Payload payload = JsonParser.parse(inputJson);
        List<Item> itemList = payload.getItemList();

        // 空列表校验
        if (itemList == null || itemList.isEmpty()) {
            System.out.println("无可用对象需处理");
            return;
        }

        // 拆分批次
        List<List<Item>> batches = BatchSplitter.split(itemList, MAX_BATCH_SIZE);
        System.out.printf("共需发起 %d 次API调用%n", batches.size());

        // 遍历批次发起调用
        for (int i = 0; i < batches.size(); i++) {
            List<Item> batch = batches.get(i);
            // 生成批次请求JSON
            String requestJson = objectMapper.writeValueAsString(new Payload(batch.size(), batch));
            // 执行API调用(替换为实际HTTP请求逻辑)
            executeApiCall(requestJson, i + 1);
        }
    }

    // 模拟API调用,实际项目替换为RestTemplate/OkHttp等客户端实现
    private static void executeApiCall(String requestJson, int batchNum) {
        System.out.printf("第 %d 次调用请求内容:%s%n", batchNum, requestJson);
        // 此处添加HTTP请求代码,比如:
        // restTemplate.postForObject("目标API地址", requestJson, 响应类型.class);
    }
}

关键说明

  • 依赖准备:需引入Jackson核心依赖(com.fasterxml.jackson.core:jackson-databind)到项目构建文件(pom.xml/build.gradle)。
  • 灵活调整:MAX_BATCH_SIZE常量可根据API限制随时修改。
  • 健壮性优化:实际项目中需补充异常捕获(如JSON解析失败、API超时等),确保流程稳定。

内容的提问来源于stack exchange,提问作者abinaya ramsamy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 08:35:01