ListMultipartFormDataMessageBodyReader implements MessageBodyReader<List<InputItem>> { @Override public List<InputItem> readFrom(Class<List<InputItem>> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException { final Type fileItemIteratorGenericType = ParameterizedTypeImpl.newParameterizedType(Iterator.class, FileItem.class); final MessageBodyReader<Iterator> multipartReader = providers.getMessageBodyReader(Iterator.class, fileItemIteratorGenericType, annotations, mediaType); final Iterator iterator = multipartReader.readFrom(Iterator.class, fileItemIteratorGenericType, annotations, mediaType, httpHeaders, entityStream); final List<InputItem> result = new LinkedList<>(); while (iterator.hasNext()) { result.add(new DefaultInputItem((FileItem)iterator.next(), providers)); } return result; } ListMultipartFormDataMessageBodyReader(@Context Providers providers); @Override boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType); @Override List<InputItem> readFrom(Class<List<InputItem>> type, Type genericType, Annotation[] annotations, MediaType mediaType,
                                    MultivaluedMap<String, String> httpHeaders, InputStream entityStream);  }
@Test public void readsListOfInputItems() throws Exception { Class type = List.class; ParameterizedType genericType = newParameterizedType(List.class, InputItem.class); Annotation[] annotations = new Annotation[0]; MediaType mediaType = MULTIPART_FORM_DATA_TYPE; MultivaluedMap<String, String> headers = new MultivaluedHashMap<>(); InputStream in = mock(InputStream.class); FileItem fileItem = createFileItem("fileItem1"); MessageBodyReader fileItemReader = createFileItemMessageBodyReader(annotations, mediaType, headers, in, fileItem); when(providers.getMessageBodyReader(eq(Iterator.class), eq(newParameterizedType(Iterator.class, FileItem.class)), aryEq(annotations), eq(mediaType))) .thenReturn(fileItemReader); List<InputItem> inputItems = listMultipartFormDataMessageBodyReader.readFrom(type, genericType, annotations, mediaType, headers, in); assertEquals(1, inputItems.size()); assertEquals(fileItem.getFieldName(), inputItems.get(0).getName()); }