本文整理汇总了Java中io.vertx.rxjava.ext.web.codec.BodyCodec类的典型用法代码示例。如果您正苦于以下问题:Java BodyCodec类的具体用法?Java BodyCodec怎么用?Java BodyCodec使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
BodyCodec类属于io.vertx.rxjava.ext.web.codec包,在下文中一共展示了BodyCodec类的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Java代码示例。
示例1: get
import io.vertx.rxjava.ext.web.codec.BodyCodec; //导入依赖的package包/类
private void get(String uri, CacheItem<JsonObject> cache, Future<JsonObject> future, Retryable retryable) {
client.get(HTTPS, ENDPOINT, uri).timeout(5000L).as(BodyCodec.jsonObject()).send(ar -> {
if (ar.succeeded()) {
HttpResponse<JsonObject> res = ar.result();
if (res.statusCode() == OK) {
future.complete(cache.set(res.body()));
} else if (res.statusCode() == RATE_LIMIT) {
retryable.retry(
() -> vertx.setTimer(getTimeTillReset(res), timer -> get(uri, cache, future, retryable)),
() -> future.fail("Too many retries."));
} else {
future.fail("TMDB API returned code: " + res.statusCode() +
"; message: " + res.statusMessage());
}
} else {
retryable.retry(
() -> vertx.setTimer(DEFAULT_DELAY, timer -> get(uri, cache, future, retryable)),
() -> future.fail("Too many failures."));
}
});
}
开发者ID:kristenkotkas,项目名称:moviediary,代码行数:22,代码来源:TmdbServiceImpl.java
示例2: testGet
import io.vertx.rxjava.ext.web.codec.BodyCodec; //导入依赖的package包/类
@Test
public void testGet() {
int times = 5;
waitFor(times);
HttpServer server = vertx.createHttpServer(new HttpServerOptions().setPort(8080));
server.requestStream().handler(req -> req.response().setChunked(true).end("some_content"));
try {
server.listen(ar -> {
client = WebClient.wrap(vertx.createHttpClient(new HttpClientOptions()));
Single<HttpResponse<Buffer>> single = client
.get(8080, "localhost", "/the_uri")
.as(BodyCodec.buffer())
.rxSend();
for (int i = 0; i < times; i++) {
single.subscribe(resp -> {
Buffer body = resp.body();
assertEquals("some_content", body.toString("UTF-8"));
complete();
}, this::fail);
}
});
await();
} finally {
server.close();
}
}
开发者ID:vert-x3,项目名称:vertx-web,代码行数:27,代码来源:RxTest.java
示例3: testResponseBodyAsAsJsonMapped
import io.vertx.rxjava.ext.web.codec.BodyCodec; //导入依赖的package包/类
@Test
public void testResponseBodyAsAsJsonMapped() throws Exception {
JsonObject expected = new JsonObject().put("cheese", "Goat Cheese").put("wine", "Condrieu");
HttpServer server = vertx.createHttpServer(new HttpServerOptions().setPort(8080));
server.requestStream().handler(req -> req.response().end(expected.encode()));
try {
server.listen(ar -> {
client = WebClient.wrap(vertx.createHttpClient(new HttpClientOptions()));
Single<HttpResponse<WineAndCheese>> single = client
.get(8080, "localhost", "/the_uri")
.as(BodyCodec.json(WineAndCheese.class))
.rxSend();
single.subscribe(resp -> {
assertEquals(200, resp.statusCode());
assertEquals(new WineAndCheese().setCheese("Goat Cheese").setWine("Condrieu"), resp.body());
testComplete();
}, this::fail);
});
await();
} finally {
server.close();
}
}
开发者ID:vert-x3,项目名称:vertx-web,代码行数:24,代码来源:RxTest.java
示例4: testErrorHandling
import io.vertx.rxjava.ext.web.codec.BodyCodec; //导入依赖的package包/类
@Test
public void testErrorHandling() throws Exception {
try {
client = WebClient.wrap(vertx.createHttpClient(new HttpClientOptions()));
Single<HttpResponse<WineAndCheese>> single = client
.get(-1, "localhost", "/the_uri")
.as(BodyCodec.json(WineAndCheese.class))
.rxSend();
single.subscribe(resp -> fail(), error -> {
assertEquals(IllegalArgumentException.class, error.getClass());
testComplete();
});
await();
} catch (Throwable t) {
fail();
}
}
开发者ID:vert-x3,项目名称:vertx-web,代码行数:18,代码来源:RxTest.java
示例5: populate
import io.vertx.rxjava.ext.web.codec.BodyCodec; //导入依赖的package包/类
private static void populate(Vertx vertx, WebClient client) {
JsonObject product1 = new JsonObject().put("name", "coffee").put("quantity", 2);
JsonObject product2 = new JsonObject().put("name", "bacon").put("quantity", 1);
JsonObject product3 = new JsonObject().put("name", "eggs").put("quantity", 3);
client.post("/shopping")
.rxSendJsonObject(product1)
.flatMap(x -> client.post("/shopping").rxSendJsonObject(product2))
.flatMap(x -> client.post("/shopping").rxSendJsonObject(product3))
.flatMap(x -> client.get("/shopping").as(BodyCodec.jsonObject()).rxSend())
.doAfterTerminate(vertx::close)
.subscribe(resp -> dump("GET /shopping", resp), ShoppingBackendClient::dump);
}
开发者ID:cescoffier,项目名称:vertx-openshift-shopping-hol,代码行数:14,代码来源:ShoppingBackendClient.java
示例6: list
import io.vertx.rxjava.ext.web.codec.BodyCodec; //导入依赖的package包/类
private static void list(Vertx vertx, WebClient client) {
client.get("/shopping")
.as(BodyCodec.jsonObject())
.rxSend()
.doAfterTerminate(vertx::close)
.subscribe(
resp -> dump("GET /shopping", resp),
ShoppingBackendClient::dump
);
}
开发者ID:cescoffier,项目名称:vertx-openshift-shopping-hol,代码行数:11,代码来源:ShoppingBackendClient.java
示例7: add
import io.vertx.rxjava.ext.web.codec.BodyCodec; //导入依赖的package包/类
private static void add(Vertx vertx, WebClient client, Args args) {
if (args.product == null || args.product.isEmpty()) {
System.out.println("Missing product name (-product)");
return;
}
JsonObject body = new JsonObject().put("name", args.product).put("quantity", args.quantity);
client.post("/shopping")
.as(BodyCodec.jsonObject())
.rxSendJsonObject(body)
.doAfterTerminate(vertx::close)
.subscribe(
resp -> dump("GET /shopping", resp),
ShoppingBackendClient::dump
);
}
开发者ID:cescoffier,项目名称:vertx-openshift-shopping-hol,代码行数:16,代码来源:ShoppingBackendClient.java
示例8: remove
import io.vertx.rxjava.ext.web.codec.BodyCodec; //导入依赖的package包/类
private static void remove(Vertx vertx, WebClient client, Args args) {
if (args.product == null || args.product.isEmpty()) {
System.out.println("Missing product name (-product)");
return;
}
client.delete("/shopping/" + args.product)
.as(BodyCodec.jsonObject())
.rxSend()
.doAfterTerminate(vertx::close)
.subscribe(
resp -> dump("DELETE /shopping/" + args.product, resp),
ShoppingBackendClient::dump
);
}
开发者ID:cescoffier,项目名称:vertx-openshift-shopping-hol,代码行数:15,代码来源:ShoppingBackendClient.java
示例9: moreComplex
import io.vertx.rxjava.ext.web.codec.BodyCodec; //导入依赖的package包/类
public void moreComplex(WebClient client) {
Single<HttpResponse<JsonObject>> single = client
.get(8080, "myserver.mycompany.com", "/some-uri")
.putHeader("some-header", "header-value")
.addQueryParam("some-param", "param value")
.as(BodyCodec.jsonObject())
.rxSend();
single.subscribe(resp -> {
System.out.println(resp.statusCode());
System.out.println(resp.body());
});
}
开发者ID:vert-x3,项目名称:vertx-web,代码行数:13,代码来源:RxWebClientExamples.java
示例10: playWithApi
import io.vertx.rxjava.ext.web.codec.BodyCodec; //导入依赖的package包/类
@Test
public void playWithApi(TestContext context) {
Async async = context.async();
JsonObject page = new JsonObject()
.put("name", "Sample")
.put("markdown", "# A page");
webClient
.get("/wiki/api/token")
.putHeader("login", "foo")
.putHeader("password", "bar")
.as(BodyCodec.string())
.rxSendJsonObject(page)
.map(r -> r.body())
.flatMap(token -> {
String jwtTokenHeaderValue = "Bearer " + token;
return webClient.post("/wiki/api/pages")
.putHeader("Authorization", jwtTokenHeaderValue)
.as(BodyCodec.jsonObject())
.rxSendJsonObject(page)
.map(r -> r.body())
.flatMap(response -> {
context.assertTrue(response.getBoolean("success"));
return webClient.get("/wiki/api/pages")
.putHeader("Authorization", jwtTokenHeaderValue)
.as(BodyCodec.jsonObject())
.rxSendJsonObject(page)
.map(r -> r.body());
}).flatMap(response -> {
context.assertTrue(response.getBoolean("success"));
JsonArray array = response.getJsonArray("pages");
context.assertEquals(1, array.size());
context.assertNotNull(array.getJsonObject(0).getInteger("id"));
return webClient.put("/wiki/api/pages/0")
.putHeader("Authorization", jwtTokenHeaderValue)
.as(BodyCodec.jsonObject())
.rxSendJsonObject(new JsonObject()
.put("id", 0)
.put("markdown", "Oh Yeah!"))
.map(r -> r.body());
}).flatMap(response -> {
context.assertTrue(response.getBoolean("success"));
return webClient.delete("/wiki/api/pages/0")
.putHeader("Authorization", jwtTokenHeaderValue)
.as(BodyCodec.jsonObject())
.rxSendJsonObject(new JsonObject()
.put("id", 0)
.put("markdown", "Oh Yeah!"))
.map(r -> r.body());
});
}).doOnError(x -> context.fail(x)).subscribe(response -> {
context.assertTrue(response.getBoolean("success"));
async.complete();
});
}
开发者ID:FroMage,项目名称:redpipe,代码行数:56,代码来源:ApiTest.java
示例11: playWithApi
import io.vertx.rxjava.ext.web.codec.BodyCodec; //导入依赖的package包/类
@Test
public void playWithApi(TestContext context) {
Async async = context.async();
JsonObject page = new JsonObject()
.put("name", "Sample")
.put("markdown", "# A page");
webClient
.get("/wiki/api/token")
.putHeader("login", "foo")
.putHeader("password", "bar")
.as(BodyCodec.string())
.rxSendJsonObject(page)
.map(r -> r.body())
.flatMap(token -> {
String jwtTokenHeaderValue = "Bearer " + token;
return webClient.post("/wiki/api/pages")
.putHeader("Authorization", jwtTokenHeaderValue)
.as(BodyCodec.jsonObject())
.rxSendJsonObject(page)
.map(r -> r.body())
.flatMap(response -> {
context.assertTrue(response.getBoolean("success"));
return webClient.get("/wiki/api/pages")
.putHeader("Authorization", jwtTokenHeaderValue)
.as(BodyCodec.jsonObject())
.rxSendJsonObject(page)
.map(r -> r.body());
}).flatMap(response -> {
context.assertTrue(response.getBoolean("success"));
JsonArray array = response.getJsonArray("pages");
context.assertEquals(1, array.size());
context.assertNotNull(array.getJsonObject(0).getInteger("id"));
return webClient.put("/wiki/api/pages/0")
.putHeader("Authorization", jwtTokenHeaderValue)
.as(BodyCodec.jsonObject())
.rxSendJsonObject(new JsonObject()
.put("id", 0)
.put("markdown", "Oh Yeah!"))
.map(r -> r.body());
}).flatMap(response -> {
context.assertTrue(response.getBoolean("success"));
return webClient.delete("/wiki/api/pages/0")
.putHeader("Authorization", jwtTokenHeaderValue)
.as(BodyCodec.jsonObject())
.rxSendJsonObject(new JsonObject()
.put("id", 0)
.put("markdown", "Oh Yeah!"))
.map(r -> r.body());
});
}).doOnError(x -> context.fail(x)).subscribe(response -> {
context.assertTrue(response.getBoolean("success"));
async.complete();
});
}
开发者ID:FroMage,项目名称:redpipe,代码行数:57,代码来源:ApiTest.java
示例12: main
import io.vertx.rxjava.ext.web.codec.BodyCodec; //导入依赖的package包/类
public static void main(String[] args) {
Vertx vertx = Vertx.vertx();
Router router = buildRouter();
EventBus eventBus = buildEventBus();
Flow flow = Flow.flow(vertx);
// Simple
flow.withDiscovery(null).route(router.get("/foo/bar"), httpFlow -> {
httpFlow.rxHttpRequest(new JsonObject(), HttpMethod.GET, "/")
.subscribe(req -> {
req.addQueryParam("foo", httpFlow.pathParam("foo")).rxSend().subscribe(resp -> {
});
});
});
// Simple with fallback
flow.withDiscovery(null).route(router.get("/foo/bar"), httpFlow -> {
httpFlow.rxHttpRequest(new JsonObject(), HttpMethod.GET, "/")
.flatMap(req -> req.addQueryParam("foo", httpFlow.pathParam("foo")).as(BodyCodec.string()).rxSend())
.map(HttpResponse::body)
.onErrorReturn(err -> "use the fallback").subscribe(s -> {
System.out.println("result is " + s);
});
});
// Nested
flow.route(router.get("/foo/bar"), httpFlow -> {
httpFlow.rxHttpRequest(new JsonObject(), HttpMethod.GET, "/").subscribe(req -> {
req.addQueryParam("foo", "bar").rxSend().subscribe(resp -> {
//
}, err -> {
httpFlow.sendMessage(new JsonObject(), "failed");
});
});
});
// Parallel
flow.from(eventBus.<String>consumer("foobar"), messageFlow -> {
messageFlow.rxHttpRequest(new JsonObject(), HttpMethod.GET, "/").subscribe(req -> {
req.addQueryParam("foo", messageFlow.body()).rxSend().subscribe(resp -> {
//
});
});
messageFlow.sendMessage(new JsonObject(), messageFlow.body());
});
}
开发者ID:vietj,项目名称:vertx-service-flow,代码行数:50,代码来源:RxExample.java
注:本文中的io.vertx.rxjava.ext.web.codec.BodyCodec类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论