org.apache.cxf.jaxrs.client.WebClient.query()方法的使用及代码示例

x33g5p2x  于2022-02-02 转载在 其他  
字(8.9k)|赞(0)|评价(0)|浏览(187)

本文整理了Java中org.apache.cxf.jaxrs.client.WebClient.query()方法的一些代码示例,展示了WebClient.query()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。WebClient.query()方法的具体详情如下:
包路径:org.apache.cxf.jaxrs.client.WebClient
类名称:WebClient
方法名:query

WebClient.query介绍

[英]Updates the current URI query parameters
[中]

代码示例

代码示例来源:origin: apache/tika

@Override
public String translate(String text, String sourceLanguage,
    String targetLanguage) throws TikaException, IOException {
  if (!this.isAvailable)
    return text;
  Response response = client.accept(MediaType.APPLICATION_JSON)
      .query("key", apiKey).query("source", sourceLanguage)
      .query("target", targetLanguage).query("q", text).get();
  BufferedReader reader = new BufferedReader(new InputStreamReader(
      (InputStream) response.getEntity(), UTF_8));
  String line = null;
  StringBuffer responseText = new StringBuffer();
  while ((line = reader.readLine()) != null) {
    responseText.append(line);
  }
  ObjectMapper mapper = new ObjectMapper();
  JsonNode jsonResp = mapper.readTree(responseText.toString());
  return jsonResp.findValuesAsText("translatedText").get(0);
}

代码示例来源:origin: apache/tika

@Override
public String translate(String text, String sourceLanguage,
            String targetLanguage) throws TikaException, IOException {
  if (!this.isAvailable)
    return text;
  Response response = client.accept(MediaType.APPLICATION_JSON)
      .query("user_key", userKey).query("source", sourceLanguage)
      .query("target", targetLanguage).query("q", text).get();
  BufferedReader reader = new BufferedReader(new InputStreamReader(
      (InputStream) response.getEntity(), UTF_8));
  String line = null;
  StringBuffer responseText = new StringBuffer();
  while ((line = reader.readLine()) != null) {
    responseText.append(line);
  }
  ObjectMapper mapper = new ObjectMapper();
  JsonNode jsonResp = mapper.readTree(responseText.toString());
  if (jsonResp.findValuesAsText("errors").isEmpty()) {
    return jsonResp.findValuesAsText("translation").get(0);
  } else {
    throw new TikaException(jsonResp.findValue("errors").get(0).asText());
  }
}

代码示例来源:origin: apache/tika

/**
 * Load the supported languages from the <a href="https://developer.lingo24.com/premium-machine-translation-api">Premium MT API</a>.
 * Support is continually expanding.
 * @return <code>Set<String></code> of supported languages.
 */
private Set<String> getAllLanguages() {
  Set<String> languages = new HashSet<>();
  if (!isAvailable) {
    return languages;
  }
  WebClient _client = null;
  try {
    _client = WebClient.create(LINGO24_TRANSLATE_URL_BASE + LINGO24_SOURCELANG_ACTION);
    Response response = _client.accept(MediaType.APPLICATION_JSON)
        .query("user_key", userKey).get();
    String json = response.readEntity(String.class);
    JsonArray jsonArray = new JsonParser().parse(json).getAsJsonObject().get("source_langs").getAsJsonArray();
    for (JsonElement jsonElement : jsonArray) {
      languages.add(jsonElement.getAsJsonArray().get(0).getAsString());
    }
  } catch (Throwable e) {
    LOG.warn("problem detecting", e);
  } finally {
    if (_client != null) {
      _client.close();
    }
  }
  return languages;
}

代码示例来源:origin: apache/tika

.query("key", this.apiKey).query("lang", langCode)
    .query("text", text).get();
BufferedReader reader = new BufferedReader(new InputStreamReader(
    (InputStream) response.getEntity(), UTF_8));

代码示例来源:origin: apache/tika

.accept("application/json").query("handler", "ignore")
    .post(attachmentPart);
reader = new InputStreamReader((InputStream) response.getEntity(), UTF_8);

代码示例来源:origin: apache/cxf

private void doTestUseParamBeanWebClient(String address) {
  WebClient wc = WebClient.create(address);
  wc.path("100").query("id_2", "20").query("id3", "3").query("id4", "123");
  Book book = wc.get(Book.class);
  assertEquals(123L, book.getId());
}

代码示例来源:origin: apache/cxf

private static boolean checkReplica(String address) {
  try {
    Response r = WebClient.create(address).query("_wadl").get();
    return r.getStatus() == 200;
  } catch (Exception ex) {
    return false;
  }
}

代码示例来源:origin: apache/cxf

@Test
public void testThatValidationConstraintsAreViolatedWhenBookNameIsNotSet()  {
  final Response r = createWebClient("/bookstore/book").query("id", "124").get();
  assertEquals(Status.INTERNAL_SERVER_ERROR.getStatusCode(), r.getStatus());
}

代码示例来源:origin: apache/cxf

@Test
public void testThatNoValidationConstraintsAreViolated()  {
  final Response r = createWebClient("/bookstore/books").query("page", "2").get();
  assertEquals(Status.OK.getStatusCode(), r.getStatus());
}

代码示例来源:origin: apache/cxf

@Test
public void testThatValidationConstraintsAreViolatedWhenBookDoesNotExist()  {
  final Response r = createWebClient("/bookstore/book").query("id", "3333").get();
  assertEquals(Status.INTERNAL_SERVER_ERROR.getStatusCode(), r.getStatus());
}

代码示例来源:origin: apache/cxf

@Test
public void testThatNoValidationConstraintsAreViolatedWhenBookIdIsSet()  {
  final Response r = createWebClient("/bookstore/book").query("id", "123").get();
  assertEquals(Status.OK.getStatusCode(), r.getStatus());
}

代码示例来源:origin: apache/cxf

@Test
public void testThatMinValidationFails()  {
  final Response r = createWebClient("/bookstore/books").query("page", "0").get();
  assertEquals(Status.BAD_REQUEST.getStatusCode(), r.getStatus());
}

代码示例来源:origin: apache/cxf

@Test
public void testQueryParameter() {
  Response r =
    createWebClient(getBasePath() + "/books/param", "text/plain").query("fieldValue", "queryParam").get();
  assertEquals("queryParam", r.readEntity(String.class));
  assertEquals(Response.Status.OK.getStatusCode(), r.getStatus());
  r = createWebClient(getBasePath() + "/books/param2", "text/plain").query("fieldValue2", "queryParam").get();
  assertEquals("queryParam", r.readEntity(String.class));
  assertEquals(Response.Status.OK.getStatusCode(), r.getStatus());
}

代码示例来源:origin: apache/cxf

private void doTestGetBookWithResourceContext(String address) throws Exception {
  WebClient wc = WebClient.create(address);
  wc.accept("application/xml");
  wc.query("bookid", "12345");
  wc.query("bookname", "bookcontext");
  Book2 book = wc.get(Book2.class);
  assertEquals(12345L, book.getId());
  assertEquals("bookcontext", book.getName());
}

代码示例来源:origin: apache/cxf

@Test
public void testGetBookJsonp() throws Exception {
  String url = "http://localhost:" + PORT + "/the/jsonp/books/123";
  WebClient client = WebClient.create(url);
  client.accept("application/json, application/x-javascript");
  client.query("_jsonp", "callback");
  Response r = client.get();
  assertEquals("application/x-javascript", r.getMetadata().getFirst("Content-Type"));
  assertEquals("callback({\"Book\":{\"id\":123,\"name\":\"CXF in Action\"}});",
         IOUtils.readStringFromStream((InputStream)r.getEntity()));
}

代码示例来源:origin: apache/cxf

@Test
public void testSearchBook123WithWebClient() throws Exception {
  String address = "http://localhost:" + PORT + "/bookstore/books/search";
  WebClient client = WebClient.create(address);
  Book b = client.query("_s", "name==CXF*;id=ge=123;id=lt=124").get(Book.class);
  assertEquals(b.getId(), 123L);
}

代码示例来源:origin: apache/cxf

@Test
  public void testSearchBook123WithWebClient() throws Exception {
    String address = "http://localhost:" + PORT + "/bookstore/books/search";

    WebClient client = WebClient.create(address);
    Book b = client.query("$filter", "name eq 'CXF*' and id ge 123 and id lt 124").get(Book.class);
    assertEquals(b.getId(), 123L);

  }
}

代码示例来源:origin: apache/cxf

@Test
public void testGetBookWithNameInQuery() throws Exception {
  String endpointAddress =
    "http://localhost:" + PORT + "/bookstore/name-in-query";
  WebClient wc = WebClient.create(endpointAddress);
  String name = "Many        spaces";
  wc.query("name", name);
  Book b = wc.get(Book.class);
  assertEquals(name, b.getName());
}

代码示例来源:origin: apache/cxf

@Test
public void testGetGenericBook() throws Exception {
  String baseAddress = "http://localhost:" + PORT + "/the/thebooks8/books";
  WebClient wc = WebClient.create(baseAddress);
  Long id = wc.type("application/xml").accept("text/plain").post(new Book("CXF", 1L), Long.class);
  assertEquals(Long.valueOf(1), id);
  Book book = wc.replaceHeader("Accept", "application/xml").query("id", 1L).get(Book.class);
  assertEquals("CXF", book.getName());
}

代码示例来源:origin: apache/cxf

@Test
public void testGetBookXSLTXml() throws Exception {
  String endpointAddress =
    "http://localhost:" + PORT + "/the/thebooks5/bookstore/books/xslt";
  WebClient wc = WebClient.create(endpointAddress);
  wc.accept("application/xml").path(666).matrix("name2", 2).query("name", "Action - ");
  Book b = wc.get(Book.class);
  assertEquals(666, b.getId());
  assertEquals("CXF in Action - 2", b.getName());
}

相关文章

WebClient类方法