如何使用 org.springframework.http.HttpHeaders 将 HTTPHeaders 的值设置为数组

How to Set the value of HTTPHeaders as Array using org.springframework.http.HttpHeaders

我正在开发 springboot 应用程序,我需要添加一个 header 来请求 API 他们使用 org.springframework.http.HttpHeaders 来设置请求的 Header 值。

我可以看到他们使用下面的代码来设置字符串值 header。

HttpHeaders headers = new HttpHeaders();
    headers.set("correlationId", "12232324545x32321");

我的要求是添加一个名为 x-ms-documentdb-partitionkey 的 header 并且它期望值是一个数组:

x-ms-documentdb-partitionkey = ["file1"]

如何使用 HttpHeaders 设置以上内容。我参考下面的 Javadocs。我想不出正确的方法。

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/http/HttpHeaders.html

谢谢

尝试在值

中明确写[]
HttpHeaders headers = new HttpHeaders();
    headers.set("x-ms-documentdb-partitionkey", "[\"file1\"]");

HTTP headers 只是 name-value 对(作为字符串)。因此,您只需将此数组以所需的表示形式设置为值即可。不要忘记正确转义"

HttpHeaders headers = new HttpHeaders();
headers.set("correlationId", "12232324545x32321");
headers.set("x-ms-documentdb-partitionkey", "[\"file1\"]");

System.out.println(headers);
[correlationId:"12232324545x32321", x-ms-documentdb-partitionkey:"["file1"]"]