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

Spring GraphQL客户端传递对象列表的实现问题求助

解决GraphQLTemplate传递ID列表的问题

一、修复当前代码的问题

你当前代码存在两个关键问题,导致无法传递ID列表:

  1. 变量名不匹配:GraphQL查询中定义的变量是$personIds,但你在variables方法里使用的变量名是"personId",名称不一致导致参数无法正确绑定。
  2. 参数类型错误:你传入的是单个字符串值,而非List<BigInteger>类型的列表。

修正后的代码如下:

@Service
public class PersonService {

    private final GraphQLTemplate graphQLTemplate = new GraphQLTemplate();
    private final String url = "http://localhost:8084/graphql";
    
    public List<Person> getPersonsByIds() {
        GraphQLRequestEntity requestEntity;
        try {
            // 准备需要查询的ID列表
            List<BigInteger> personIdList = Arrays.asList(
                new BigInteger("2477142261427744786"),
                new BigInteger("1234567890123456789")
            );
            
            requestEntity = GraphQLRequestEntity.Builder()
                .url(url)
                .requestMethod(GraphQLTemplate.GraphQLMethod.QUERY)
                .request("query($personIds: [BigInteger]) {
" +
                    "  getPersonsByIds(personIds : $personIds ) {
" +
                    "    firstName
" +
                    "    middleName
" +
                    "    lastName
" +
                    "    birthDt
" +
                    "  }
" +
                    "}"
                )
                // 修正变量名并传入列表参数
                .variables(new Variable<>("personIds", personIdList))
                .build();
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }
        return graphQLTemplate.query(requestEntity, ResponseGetPersonsByIds.class).getResponse().getGetPersonsByIds();
    }    
}

同时需要确保ResponseGetPersonsByIds的结构正确,其getGetPersonsByIds()方法需返回List<Person>类型:

public class ResponseGetPersonsByIds {
    private List<Person> getPersonsByIds;

    public List<Person> getGetPersonsByIds() {
        return getPersonsByIds;
    }

    public void setGetPersonsByIds(List<Person> getPersonsByIds) {
        this.getPersonsByIds = getPersonsByIds;
    }
}

二、关于客户端库的选择

你使用的com.github.americanexpress:nodes:0.5.0版本过于老旧(最后更新在2019年),社区维护和文档支持都很有限,建议更换为以下主流的Spring GraphQL客户端:

1. Spring官方Spring GraphQL Client

与Spring生态深度整合,配置简单且文档完善,是首选方案。
首先添加Gradle依赖:

implementation 'org.springframework.boot:spring-boot-starter-graphql'

示例客户端代码:

@Service
public class PersonService {
    private final GraphQlClient graphQlClient;

    // 构造注入GraphQlClient
    public PersonService(GraphQlClient.Builder clientBuilder) {
        this.graphQlClient = clientBuilder.url("http://localhost:8084/graphql").build();
    }

    public List<Person> getPersonsByIds() {
        List<BigInteger> personIdList = Arrays.asList(
            new BigInteger("2477142261427744786"),
            new BigInteger("1234567890123456789")
        );

        return graphQlClient.document("""
                query getPersonsByIds($personIds: [BigInteger]) {
                    getPersonsByIds(personIds: $personIds) {
                        firstName
                        middleName
                        lastName
                        birthDt
                    }
                }
                """)
                .variable("personIds", personIdList)
                .retrieve("getPersonsByIds")
                .toEntityList(Person.class)
                .block();
    }
}

2. Netflix DGS Client

Netflix推出的DGS框架配套客户端,支持代码生成等高级特性,适合复杂业务场景。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 00:35:17