JAX-RS @HeaderParam with List, populated with only one comma separated element

六眼飞鱼酱① 提交于 2019-12-06 09:23:51

问题


This is a JAX-RS specific question. According to @HeaderParam docs:

https://docs.oracle.com/javaee/7/api/javax/ws/rs/HeaderParam.html

Be List, Set or SortedSet, where T satisfies 2, 3 or 4 above. The resulting collection is read-only. If the type is not one of the collection types listed in 5 above and the header parameter is represented by multiple values then the first value (lexically) of the parameter is used.

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";
  }

}

The client:

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  

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>

Thanks


回答1:


This is not a bug of your application. It works as designed. Multiple header parameters are comma separated.

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.



来源:https://stackoverflow.com/questions/42884930/jax-rs-headerparam-with-list-populated-with-only-one-comma-separated-element

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!