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

Spring Boot中DGS GraphQlClient请求返回403,HttpClient正常

解决DGS GraphQL Client请求返回403 Forbidden的问题

问题原因

目标GraphQL API服务器不接受分块传输(Transfer-Encoding: chunked)格式的请求,而Netflix DGS GraphQL Client默认采用这种传输方式发送请求——这和你用Java HttpClient时通过Content-Length指定内容长度的请求方式存在差异,最终导致服务器返回403拒绝响应。

解决办法

DGS Client底层依赖Spring WebClient,只需通过配置关闭分块传输,强制使用Content-Length即可解决问题:

方法一:自定义WebClient Bean

编写配置类替换默认的WebClient,禁用分块编码:

@Configuration
public class DgsClientConfiguration {

    @Bean
    public WebClient dgsWebClient() {
        return WebClient.builder()
                .codecs(codecConfigurer -> {
                    codecConfigurer.defaultCodecs().jackson2JsonEncoder(new Jackson2JsonEncoder() {
                        @Override
                        public Mono<Void> encode(Publisher<?> inputStream, DataBuffer bufferFactory, ResolvableType elementType, 
                                                @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {
                            // 添加强制关闭分块的参数
                            Map<String, Object> modifiedHints = new HashMap<>(hints != null ? hints : Collections.emptyMap());
                            modifiedHints.put(ClientCodecConfigurer.ENABLE_CHUNKED_ENCODING, false);
                            return super.encode(inputStream, bufferFactory, elementType, mimeType, modifiedHints);
                        }
                    });
                })
                .build();
    }

    @Bean
    public DgsGraphQLClient dgsGraphQLClient(DgsClientProperties properties, WebClient dgsWebClient) {
        return DefaultDgsGraphQLClient.builder()
                .webClient(dgsWebClient)
                .url(properties.getGraphQlEndpoint())
                .build();
    }
}

方法二:配置文件直接关闭分块(适用于新版DGS)

如果使用的是较新版本的DGS框架,可直接通过配置文件修改:

# application.properties
spring.graphql.client.dgs.chunked-encoding=false

或YAML格式:

# application.yml
spring:
  graphql:
    client:
      dgs:
        chunked-encoding: false

验证

修改完成后重新发送请求,通过抓包工具确认:请求头不再包含Transfer-Encoding: chunked,而是出现Content-Length字段;请求体也不再有额外的分块标记(如"41"、"0")。此时请求应该能正常获取服务器响应。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.12 23:28:17