JsonSerializerMiddlewares { public static <T> Middleware<AsyncHandler<Response<T>>, AsyncHandler<Response<ByteString>>> jsonSerializeResponse(ObjectWriter objectWriter) { return handler -> requestContext -> handler.invoke(requestContext) .thenApply(response -> serialize(objectWriter, response) .withHeader(CONTENT_TYPE, JSON)); } private  JsonSerializerMiddlewares(); static Middleware<AsyncHandler<T>, AsyncHandler<Response<ByteString>>> jsonSerialize(ObjectWriter objectWriter); static Middleware<AsyncHandler<Response<T>>, AsyncHandler<Response<ByteString>>> jsonSerializeResponse(ObjectWriter objectWriter); static Middleware<SyncHandler<T>, AsyncHandler<Response<ByteString>>> jsonSerializeSync(ObjectWriter objectWriter); static Middleware<SyncHandler<Response<T>>, AsyncHandler<Response<ByteString>>> jsonSerializeResponseSync(ObjectWriter objectWriter);  }
@Test public void shouldJsonSerializeResponse() throws Exception { Response<ByteString> response = Route.sync("GET", "/foo", rq -> Response.forStatus(Status.CONFLICT).withPayload(new TestBean())) .withMiddleware(JsonSerializerMiddlewares.jsonSerializeResponse(WRITER)) .handler().invoke(null).toCompletableFuture().get(); checkPayloadAndContentType(response); assertThat(response.status(), equalTo(Status.CONFLICT)); }
@Test public void shouldJsonSerializeEmptyResponse() throws Exception { Response<ByteString> response = Route.sync("GET", "/foo", rq -> Response.forStatus(Status.CONFLICT)) .withMiddleware(JsonSerializerMiddlewares.jsonSerializeResponse(WRITER)) .handler().invoke(null).toCompletableFuture().get(); checkContentType(response); assertThat(response.payload(), equalTo(Optional.empty())); assertThat(response.status(), equalTo(Status.CONFLICT)); }