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

1
  • Hello, Is there any solution to the problem. I am trying the same thing but with the baggage header w3.org/TR/baggage and still the comma separated key-value pairs are not being separated into different List elements? Commented Apr 8, 2021 at 19:13

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

A similar response is provided on Jersey issue 2436.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.