Middlewares { static Middleware<AsyncHandler<? extends Response<?>>, AsyncHandler<Response<ByteString>>> jsonAsync() { return innerHandler -> innerHandler.map(response -> { if (response.payload().isEmpty()) { return (Response<ByteString>) response; } final Object tPayload = response.payload().get(); try { final byte[] bytes = OBJECT_MAPPER.writeValueAsBytes(tPayload); final ByteString payload = ByteString.of(bytes); return response.withPayload(payload) .withHeader("Content-Type", "application/json"); } catch (JsonProcessingException e) { return Response.forStatus( INTERNAL_SERVER_ERROR.withReasonPhrase( "Failed to serialize response " + e.getMessage())); } }); } private  Middlewares(); static Middleware<SyncHandler<? extends Response<?>>, AsyncHandler<Response<ByteString>>> json(); static Middleware<AsyncHandler<Response<T>>, AsyncHandler<Response<T>>> clientValidator(
      Supplier<Set<String>> supplier); static Middleware<AsyncHandler<Response<T>>, AsyncHandler<Response<T>>> exceptionAndRequestIdHandler(); static Middleware<AsyncHandler<Response<T>>, AsyncHandler<Response<T>>> tracer(
      Tracer tracer, String service); static Middleware<Requested<Authenticated<Response<T>>>, SyncHandler<Response<T>>> authed(
      RequestAuthenticator authenticator); static Middleware<Authenticated<EntityResponseHandler<E, R>>, SyncHandler<Response<ByteString>>> authedEntity(
      Middleware<EntityResponseHandler<E, R>, SyncHandler<Response<ByteString>>> entityMiddleware,
      Middleware<Requested<Authenticated<Response<ByteString>>>, SyncHandler<Response<ByteString>>> authed); static Middleware<Authenticated<EntityResponseHandler<E, R>>, SyncHandler<Response<ByteString>>> authedEntity(
      RequestAuthenticator authenticator,
      Middleware<EntityResponseHandler<E, R>, SyncHandler<Response<ByteString>>> entityMiddleware); static Middleware<AsyncHandler<Response<T>>, AsyncHandler<Response<T>>> authenticator(
      RequestAuthenticator authenticator);  }
@Test public void testJsonAsync() throws Exception { AsyncHandler<Response<ByteString>> outerHandler = Middlewares.jsonAsync().apply( rc -> completedFuture(Response.forPayload(TEST_STRUCT)) ); CompletionStage<Response<ByteString>> completionStage = outerHandler.invoke( RequestContexts.create(mock(Request.class), mock(Client.class), Collections.emptyMap(), System.nanoTime(), RequestMetadataImpl.create(Instant.EPOCH, Optional.empty(), Optional.empty()))); assertThat(completionStage.toCompletableFuture().get().payload().orElseThrow().utf8(), is( "{\"foo\":\"blah\"," + "\"inner_object\":{" + "\"field_name_convention\":\"bloh\"," + "\"enum_field\":\"enum_value\"}}" )); }