Jersey uses MultivaluedMaps to represent request data but other places (e.g. - Java javax.ws.rs.core

Java examples for javax.ws.rs.core:MultivaluedMap

Description

Jersey uses MultivaluedMaps to represent request data but other places (e.g.

Demo Code


import javax.ws.rs.core.MultivaluedMap;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class Main{
    /**/*  w  w w.  j a v  a 2 s  . c o  m*/
         * Jersey uses MultivaluedMaps to represent request data but other places (e.g. Twilio client ) need 1:1 maps
         * This method just takes the last item for each parameter value.
         * @param incoming the map of values lists
         * @return the simplified map
         */
        public static <K, V> Map<K, V> simplifyMultivaluedMap(
                MultivaluedMap<K, V> incoming) {
            Map<K, V> params = new HashMap<>();
            for (Map.Entry<K, List<V>> entry : incoming.entrySet()) {
                List<V> values = entry.getValue();

                if (values.size() > 0) {
                    params.put(entry.getKey(), values.get(values.size() - 1));
                }
            }

            return params;
        }
}

Related Tutorials