2

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

mortalis
  • 2,060
  • 24
  • 34
Joseph Mae
  • 47
  • 2
  • 7
  • Hello, Is there any solution to the problem. I am trying the same thing but with the baggage header https://www.w3.org/TR/baggage/ and still the comma separated key-value pairs are not being separated into different List elements? – Yash Bansal Apr 08 '21 at 19:13

2 Answers2

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.

Community
  • 1
  • 1
Christian13467
  • 5,324
  • 31
  • 34
  • A similar response is provided on Jersey [issue 2436](https://github.com/jersey/jersey/issues/2436). – kfateem Sep 15 '17 at 08:58
0

Looks like a bug to me, but they're claiming that this is how they intend Jersey to function (per https://github.com/jersey/jersey/issues/2436). The reasoning seems questionable to me. RFC 2616 is not as simple as they imply. What RFC 2616 says is essentially that a header can be present multiple times IF it can be treated as semantically equivalent to a comma separated list. Whether a comma separated list should be treated as a List seems entirely up to the developer and it seems rather straightforward that your clear use of the List type and the HeaderParam annotation signals that intention.