What is the use of the Java 6 MultivaluedMap interface?

What is the Java 6 MultivaluedMap interface?

+8
java data-structures jax-rs java-6
source share
4 answers

The interface does not belong to Java, which means that the interface is not part of the core libraries. It is part of the javax.ws.rs hierarchy, which is part of the JAX-RS specification. It is used by entities implementing the specification, such as Jersey . It is used when cards must refer not only to one value, but also to any number of values. An example of a use could be, for example, storing a request header in which you can add several values ​​for each key. Or even no keys in some cases where it is easier to process an empty list compared to null .

Take this HTTP header, for example:

Accept-Encoding: compress; q = 0.5, gzip; q = 1.0

Would you simulate this with

 MultivaluedMap<String, String> map = ... map.add("Accept-Encoding", "compress;q=0.5"); map.add("Accept-Encoding", "gzip;q=1.0"); 

internally in jersey. This type of multi-value storage is a common problem in Java that other map developers, such as Guava , are accessing .

This is basically what javadoc says:

Map of key-value pairs. Each key may have zero or more values.

+19
source share

Its a map of value-value pairs. Each key can have zero or more values.

 public interface MultivaluedMap<K,V> extends java.util.Map<K,java.util.List<V>> 
+1
source share

Good use of MultivaluedMap with UriInfo. If you are writing a REST endpoint that accepts multiple QueryParams, you can use UriInfo to get all the parameters and retrieve them with a call to getQuery (). For example:

 public void get(@Context UriInfo ui) { MultivaluedMap params = ui.getRequestUri().getQuery(); // now do what you want with your params } 

MultivaluedMap is useful because you can have parameters with multiple values. For example, if it is a customer database and you want to get several customers, your card will have the key "customerID" and several values ​​associated with it.

+1
source share

MultiValuedMap is part of the javax.ws.rs.core package, not Core Java. It is mainly used to store the value of headers in a request and use it

private MediaType getMediaType (class entityClass, type entityType, multivalued headers) {final Object mediaTypeHeader = headers.getFirst ("Content-Type"); ....}

It is also very useful in UriInfo.

 private String getJsonpFunctionName(){ UriInfo uriInfo = getUriInfo(); if (uriInfo == null) { return null; } MultivaluedMap<String, String> queryParameters = uriInfo.getQueryParameters(); if (queryParameters == null) { return null; } return queryParameters.getFirst("jsonp"); } 
0
source share

All Articles