MultivaluedMapEntityProvider implements EntityProvider<MultivaluedMap<String, String>> { @Override public void writeTo(MultivaluedMap<String, String> multivaluedMap, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException { int i = 0; for (Map.Entry<String, List<String>> e : multivaluedMap.entrySet()) { for (String value : e.getValue()) { if (i > 0) { entityStream.write('&'); } String name = URLEncoder.encode(e.getKey(), "UTF-8"); entityStream.write(name.getBytes()); i++; if (value != null) { entityStream.write('='); value = URLEncoder.encode(value, "UTF-8"); entityStream.write(value.getBytes()); } } } } MultivaluedMapEntityProvider(@Context HttpServletRequest httpRequest); @Override boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType); @Override @SuppressWarnings("unchecked") MultivaluedMap<String, String> readFrom(Class<MultivaluedMap<String, String>> type,
                                                   Type genericType,
                                                   Annotation[] annotations,
                                                   MediaType mediaType,
                                                   MultivaluedMap<String, String> httpHeaders,
                                                   InputStream entityStream); @Override long getSize(MultivaluedMap<String, String> multivaluedMap, Class<?> type, Type genericType, Annotation[] annotations,
                        MediaType mediaType); @Override boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType); @Override void writeTo(MultivaluedMap<String, String> multivaluedMap,
                        Class<?> type,
                        Type genericType,
                        Annotation[] annotations,
                        MediaType mediaType,
                        MultivaluedMap<String, Object> httpHeaders,
                        OutputStream entityStream);  }
@Test public void writesMultivaluedMapToOutputStream() throws Exception { MultivaluedMap<String, String> multivaluedMap = new MultivaluedHashMap<>(); multivaluedMap.put("bar", newArrayList("hello world")); multivaluedMap.put("foo", newArrayList("to be or not to be")); ByteArrayOutputStream out = new ByteArrayOutputStream(); multivaluedMapEntityProvider.writeTo(multivaluedMap, MultivaluedMap.class, multiValuedMapGenericType, null, APPLICATION_FORM_URLENCODED_TYPE, new MultivaluedHashMap<>(), out); assertEquals("bar=hello+world&foo=to+be+or+not+to+be", out.toString()); }