SerDesMiddleware implements Middleware<HttpRequest, HttpResponse, HttpRequest, NRES> { @SuppressWarnings("unchecked") protected <T> T deserialize(HttpRequest request, Class<T> type, Type genericType, MediaType mediaType) throws IOException { try { MultivaluedMap<String, Object> headers = new MultivaluedHashMap<>(); return bodyReaders.stream() .filter(reader -> reader.isReadable(type, genericType, null, mediaType)) .map(reader -> { try { return (T) MessageBodyReader.class.cast(reader) .readFrom(type, genericType, null, mediaType, headers, request.getBody()); } catch (IOException e) { throw new UncheckedIOException(e); } }) .filter(Objects::nonNull) .findAny() .orElse(null); } catch (UncheckedIOException e) { throw e.getCause(); } }  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 test() throws IOException { SerDesMiddleware<Object> middleware = builder(new SerDesMiddleware<>()) .set(SerDesMiddleware::setBodyReaders, jsonProvider) .set(SerDesMiddleware::setBodyWriters, jsonProvider) .build(); String body = "{\"a\":1, \"b\":\"ccb\"}"; HttpRequest request = builder(new DefaultHttpRequest()) .set(HttpRequest::setHeaders, Headers.of("Content-Type", "application/json")) .set(HttpRequest::setBody, new ByteArrayInputStream(body.getBytes())) .build(); TestDto testDto = middleware.deserialize(request, TestDto.class, TestDto.class, new MediaType("application", "json")); assertThat(testDto.getA()).isEqualTo(1); assertThat(testDto.getB()).isEqualTo("ccb"); }
@Test public void deserializeList() throws IOException { SerDesMiddleware<Object> middleware = builder(new SerDesMiddleware<>()) .set(SerDesMiddleware::setBodyReaders, jsonProvider) .set(SerDesMiddleware::setBodyWriters, jsonProvider) .build(); String body = "[{\"a\":1, \"b\":\"ccb\"}, {\"a\":2, \"b\":\"ooo\"}]"; HttpRequest request = builder(new DefaultHttpRequest()) .set(HttpRequest::setHeaders, Headers.of("Content-Type", "application/json")) .set(HttpRequest::setBody, new ByteArrayInputStream(body.getBytes())) .build(); Object testDto = middleware.deserialize(request, List.class, new TypeReference<List<TestDto>>(){}.getType(), new MediaType("application", "json")); assertThat(testDto).isInstanceOf(List.class); assertThat(testDto).asList().size().isEqualTo(2); assertThat(testDto).asList().contains(builder(new TestDto()) .set(TestDto::setA, 1) .set(TestDto::setB, "ccb") .build()); }
@Test public void deserializeUnknownListOrMap() throws IOException { SerDesMiddleware<Object> middleware = builder(new SerDesMiddleware<>()) .set(SerDesMiddleware::setBodyReaders, jsonProvider) .set(SerDesMiddleware::setBodyWriters, jsonProvider) .build(); String body = "[{\"a\":1, \"b\":\"ccb\"}, {\"a\":2, \"b\":\"ooo\"}]"; HttpRequest request = builder(new DefaultHttpRequest()) .set(HttpRequest::setHeaders, Headers.of("Content-Type", "application/json")) .set(HttpRequest::setBody, new ByteArrayInputStream(body.getBytes())) .build(); Object testDto = middleware.deserialize(request, Object.class, Object.class, new MediaType("application", "json")); assertThat(testDto).isInstanceOf(List.class); assertThat(testDto).asList().size().isEqualTo(2); }
@Test public void raiseException() { assertThatThrownBy(() -> { SerDesMiddleware<Object> middleware = builder(new SerDesMiddleware<>()) .set(SerDesMiddleware::setBodyReaders, jsonProvider) .set(SerDesMiddleware::setBodyWriters, jsonProvider) .build(); String body = "{\"a\":1, \"b\":\"ccb\", \"c\": \"This is an unknown property.\"}"; HttpRequest request = builder(new DefaultHttpRequest()) .set(HttpRequest::setHeaders, Headers.of("Content-Type", "application/json")) .set(HttpRequest::setBody, new ByteArrayInputStream(body.getBytes())) .build(); middleware.deserialize(request, TestDto.class, TestDto.class, new MediaType("application", "json")); }).isInstanceOf(UnrecognizedPropertyException.class); }