WebServiceClient { <T> void putRequestBean(HTTPRequest request, Class<T> requestBeanClass, T requestBean) { HTTPMethod method = request.method; if (method == HTTPMethod.GET || method == HTTPMethod.DELETE) { Map<String, String> queryParams = writer.toParams(requestBeanClass, requestBean); request.params.putAll(queryParams); } else if (method == HTTPMethod.POST || method == HTTPMethod.PUT || method == HTTPMethod.PATCH) { byte[] json = writer.toJSON(requestBeanClass, requestBean); request.body(json, ContentType.APPLICATION_JSON); } else { throw new Error("not supported method, method=" + method); } } WebServiceClient(String serviceURL, HTTPClient httpClient, RequestBeanWriter writer, ResponseBeanReader reader); Object execute(HTTPMethod method, String path, Class<T> requestBeanClass, T requestBean, Type responseType); void intercept(WebServiceClientInterceptor interceptor); void logCallWebService(String method); static final String USER_AGENT; }
@Test void putRequestBeanWithGet() { var request = new HTTPRequest(HTTPMethod.GET, "/"); var requestBean = new TestWebService.TestSearchRequest(); requestBean.intField = 23; webServiceClient.putRequestBean(request, TestWebService.TestSearchRequest.class, requestBean); assertThat(request.params).contains(entry("int_field", "23")); }
@Test void putRequestBeanWithPost() { var request = new HTTPRequest(HTTPMethod.POST, "/"); var requestBean = new TestWebService.TestRequest(); requestBean.stringField = "123value"; webServiceClient.putRequestBean(request, TestWebService.TestRequest.class, requestBean); assertThat(new String(request.body, UTF_8)).isEqualTo(JSON.toJSON(requestBean)); assertThat(request.contentType).isEqualTo(ContentType.APPLICATION_JSON); }