SerDesMiddleware implements Middleware<HttpRequest, HttpResponse, HttpRequest, NRES> { @SuppressWarnings("unchecked") protected InputStream serialize(Object obj, MediaType mediaType) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); MultivaluedMap<String, Object> headers = new MultivaluedHashMap<>(); if (obj == null) { return new ByteArrayInputStream(new byte[0]); } return bodyWriters.stream() .sorted((writer1, writer2) -> Boolean.compare( writer1.isWriteable(obj.getClass(), obj.getClass(), null, mediaType), writer2.isWriteable(obj.getClass(), obj.getClass(), null, mediaType))) .map(writer -> { try { MessageBodyWriter.class.cast(writer).writeTo(obj, obj.getClass(), obj.getClass(), null, mediaType, headers, baos); return baos.toByteArray(); } catch (IOException e) { return null; } }) .filter(Objects::nonNull) .map(ByteArrayInputStream::new) .findFirst() .orElse(null); }  void handleRequest(HttpRequest request); @Override HttpResponse handle(HttpRequest request, MiddlewareChain<HttpRequest, NRES, ?, ?> chain); void setBodyReaders(MessageBodyReader<?>... readers); void setBodyWriters(MessageBodyWriter<?>... writers); void setParameterInjectors(List<ParameterInjector<?>> parameterInjectors);  }
@Test public void nullSerialize() { SerDesMiddleware<Object> middleware = builder(new SerDesMiddleware<>()) .set(SerDesMiddleware::setBodyReaders, jsonProvider) .build(); middleware.serialize(null, new MediaType("application", "json")); }