问题描述
这是JAX-RS的特定问题.根据@HeaderParam
文档:
This is a JAX-RS specific question. According to @HeaderParam
docs:
https://docs.oracle.com /javaee/7/api/javax/ws/rs/HeaderParam.html
从文档中可以明显看出,如果标头有多个值,则可以将其映射到集合.这是我的示例:
It's clear from the docs that if there are multiple values for a header then it can be mapped to a collection.Here's my example:
@Path("/")
public class TestResource {
@GET
@Path("test")
public String test(@HeaderParam("myHeader") List<String> list) {
System.out.println(list.size());
list.stream().forEach(System.out::println);
return "response";
}
}
客户:
Client client = ClientBuilder.newClient();
WebTarget target = client.target("http://localhost:8080/test");
String response = target.request()
.header("myHeader", "a")
.header("myHeader", "b")
.header("myHeader", "c,d")
.get(String.class);
client.close();
服务器控制台上的输出:
output on the server console:
1
a,b,c,d
只有一个元素填充为"a,b,c,d",而不是4个单独的元素.我在这里想念什么?搜索问题,但未找到任何答案.我正在使用Jersey 2.25.1.并在嵌入式tomcat中运行它:
Only one element is populated 'a,b,c,d' instead of 4 separate elements.What am I missing here? Googled the problem but didn't find any answers.I'm using Jersey 2.25.1. and running it in embedded tomcat:
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.25.1</version>
</dependency>
<!-- ............... -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<path>/</path>
</configuration>
</plugin>
谢谢
推荐答案
这不是您的应用程序的错误.它按设计工作.多个标头参数以逗号分隔.
This is not a bug of your application. It works as designed. Multiple header parameters are comma separated.
查看,它引用了HTTP协议rfc来使用多个标头属性.
Look at Standard for adding multiple values of a single HTTP Header to a request or response It references http protocol rfc for usage of multiple header attributes.
这篇关于具有列表的JAX-RS @HeaderParam,仅填充一个逗号分隔的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!