本文整理汇总了Java中io.vertx.ext.web.codec.BodyCodec类的典型用法代码示例。如果您正苦于以下问题:Java BodyCodec类的具体用法?Java BodyCodec怎么用?Java BodyCodec使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
BodyCodec类属于io.vertx.ext.web.codec包,在下文中一共展示了BodyCodec类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: buildRequest
import io.vertx.ext.web.codec.BodyCodec; //导入依赖的package包/类
private HttpRequest<JsonObject> buildRequest(String requestURI, RequestParameters parameters) {
HttpRequest<JsonObject> request = client.post(requestURI)
.as(BodyCodec.jsonObject())
.putHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8")
.setQueryParam("properties", parameters.getProperties().toString())
.setQueryParam("pipelineLanguage", parameters.getLanguage().name());
if (options.getUsername() != null && options.getPassword() != null) {
request.putHeader("Authorization",
"Basic " + Base64.getEncoder().encodeToString(String.join(":", options.getUsername(), options.getPassword()).getBytes()));
}
if (parameters.getPattern() != null) {
request.setQueryParam("pattern", parameters.getPattern())
.setQueryParam("filter", String.valueOf(parameters.isFilter()));
}
return request;
}
示例2: getAddressSpace
import io.vertx.ext.web.codec.BodyCodec; //导入依赖的package包/类
/**
* get address space by address space name vie rest api
*
* @param name name of address space
* @return
* @throws InterruptedException
*/
public JsonObject getAddressSpace(String name) throws Exception {
CompletableFuture<JsonObject> responsePromise = new CompletableFuture<>();
return doRequestNTimes(initRetry, () -> {
client.get(endpoint.getPort(), endpoint.getHost(), addressSpacesPath + "/" + name)
.as(BodyCodec.jsonObject())
.putHeader(HttpHeaders.AUTHORIZATION.toString(), authzString)
.send(ar -> {
if (ar.succeeded()) {
responsePromise.complete(responseHandler(ar));
} else {
Logging.log.warn("Error when getting address space {}", name);
responsePromise.completeExceptionally(ar.cause());
}
});
return responsePromise.get(30, TimeUnit.SECONDS);
});
}
示例3: getValueForCompany
import io.vertx.ext.web.codec.BodyCodec; //导入依赖的package包/类
private Future<Double> getValueForCompany(WebClient client, String company, int numberOfShares) {
// Create the future object that will get the value once the value have been retrieved
Future<Double> future = Future.future();
//----
client.get("/?name=" + encode(company))
.as(BodyCodec.jsonObject())
.send(ar -> {
if (ar.succeeded()) {
HttpResponse<JsonObject> response = ar.result();
if (response.statusCode() == 200) {
double v = numberOfShares * response.body().getDouble("bid");
future.complete(v);
} else {
future.complete(0.0);
}
} else {
future.fail(ar.cause());
}
});
// ---
return future;
}
示例4: testInsertion
import io.vertx.ext.web.codec.BodyCodec; //导入依赖的package包/类
@Test
public void testInsertion(TestContext tc) {
Async async = tc.async();
vertx.eventBus().publish("portfolio", createBuyOperation());
WebClient.create(vertx).get(8081, "localhost", "/")
.as(BodyCodec.jsonArray())
.send(tc.asyncAssertSuccess(response -> {
tc.assertEquals(response.statusCode(), 200);
tc.assertEquals(response.body().size(), 1);
async.complete();
}));
}
示例5: receiveResponseAsJsonObject
import io.vertx.ext.web.codec.BodyCodec; //导入依赖的package包/类
public void receiveResponseAsJsonObject(WebClient client) {
client
.get(8080, "myserver.mycompany.com", "/some-uri")
.as(BodyCodec.jsonObject())
.send(ar -> {
if (ar.succeeded()) {
HttpResponse<JsonObject> response = ar.result();
JsonObject body = response.body();
System.out.println("Received response with status code" + response.statusCode() + " with body " + body);
} else {
System.out.println("Something went wrong " + ar.cause().getMessage());
}
});
}
示例6: receiveResponseAsJsonPOJO
import io.vertx.ext.web.codec.BodyCodec; //导入依赖的package包/类
public void receiveResponseAsJsonPOJO(WebClient client) {
client
.get(8080, "myserver.mycompany.com", "/some-uri")
.as(BodyCodec.json(User.class))
.send(ar -> {
if (ar.succeeded()) {
HttpResponse<User> response = ar.result();
User user = response.body();
System.out.println("Received response with status code" + response.statusCode() + " with body " +
user.getFirstName() + " " + user.getLastName());
} else {
System.out.println("Something went wrong " + ar.cause().getMessage());
}
});
}
示例7: HttpRequestImpl
import io.vertx.ext.web.codec.BodyCodec; //导入依赖的package包/类
HttpRequestImpl(WebClientImpl client, HttpMethod method, String protocol, boolean ssl, int port, String host, String
uri, BodyCodec<T> codec, WebClientOptions options) {
this.client = client;
this.method = method;
this.protocol = protocol;
this.codec = codec;
this.port = port;
this.host = host;
this.uri = uri;
this.ssl = ssl;
this.followRedirects = options.isFollowRedirects();
this.options = options;
if (options.isUserAgentEnabled()) {
headers = new CaseInsensitiveHeaders().add(HttpHeaders.USER_AGENT, options.getUserAgent());
}
}
示例8: subscriptionFetch
import io.vertx.ext.web.codec.BodyCodec; //导入依赖的package包/类
private void subscriptionFetch() {
if (this.shuttingDown || this.shutdownCompleted) {
this.shutdownCompleted = true;
return;
}
this.logger.info("Fetch " + Utils.getDateString(new Date()));
final JsonArray body = this.getConnectBody();
final HttpRequest<Buffer> request = this.initWebPostRequest(Constants.URL_CONNECT);
request.as(BodyCodec.jsonArray()).sendJson(body, this::subscriptionResult);
}
示例9: shouldFindAll
import io.vertx.ext.web.codec.BodyCodec; //导入依赖的package包/类
@Test
public void shouldFindAll(TestContext context) {
Async async = context.async();
webClient.get("/api/dependencies").as(BodyCodec.jsonArray()).send(response -> {
if (response.succeeded()) {
assertThat(response.result().statusCode(), is(200));
JsonArray payload = response.result().body();
assertThat(payload.size(), is(2));
async.complete();
} else {
context.fail(response.cause());
async.complete();
}
});
}
示例10: shouldFindAll
import io.vertx.ext.web.codec.BodyCodec; //导入依赖的package包/类
@Test
public void shouldFindAll(TestContext context) {
Async async = context.async();
webClient.get("/api/versions").as(BodyCodec.jsonArray()).send(response -> {
if (response.succeeded()) {
assertThat(response.result().statusCode(), is(200));
JsonArray payload = response.result().body();
assertThat(payload.size(), is(4));
async.complete();
} else {
context.fail(response.cause());
async.complete();
}
});
}
示例11: testRecordCreation
import io.vertx.ext.web.codec.BodyCodec; //导入依赖的package包/类
@Test
public void testRecordCreation(TestContext testContext) {
Record record = HttpEndpoint.createRecord("some-name", "123.456.789.111", 80, null);
assertThat(record.getLocation().getString(Record.ENDPOINT)).isEqualTo("http://123.456.789.111:80/");
record = HttpEndpoint.createRecord("some-name", "123.456.789.111", 80, "foo");
assertThat(record.getLocation().getString(Record.ENDPOINT)).isEqualTo("http://123.456.789.111:80/foo");
record = HttpEndpoint.createRecord("some-name", "123.456.789.111", 80, "foo", new JsonObject().put("language", "en"));
assertThat(record.getLocation().getString(Record.ENDPOINT)).isEqualTo("http://123.456.789.111:80/foo");
assertThat(record.getMetadata().getString("language")).isEqualTo("en");
record = HttpEndpoint.createRecord("some-name", "acme.org");
assertThat(record.getLocation().getString(Record.ENDPOINT)).isEqualTo("http://acme.org:80/");
SelfSignedCertificate selfSignedCertificate = SelfSignedCertificate.create();
vertx.createHttpServer(new HttpServerOptions()
.setHost("127.0.0.1")
.setSsl(true)
.setKeyCertOptions(selfSignedCertificate.keyCertOptions())
).requestHandler(request -> {
request.response().end(new JsonObject().put("url", request.absoluteURI()).encode());
}).listen(0, testContext.asyncAssertSuccess(server -> {
Record sslRecord = HttpEndpoint.createRecord("http-bin", true, "127.0.0.1", server.actualPort(), "/get", null);
ServiceReference reference = discovery.getReferenceWithConfiguration(sslRecord, new HttpClientOptions()
.setSsl(true)
.setTrustAll(true)
.setVerifyHost(false)
.toJson());
WebClient webClient = WebClient.wrap(reference.get());
webClient.get("/get").as(BodyCodec.jsonObject()).send(testContext.asyncAssertSuccess(resp -> {
assertEquals("https://127.0.0.1:" + server.actualPort() + "/get", resp.body().getString("url"));
}));
}));
}
示例12: receiveResponseAsWriteStream
import io.vertx.ext.web.codec.BodyCodec; //导入依赖的package包/类
public void receiveResponseAsWriteStream(WebClient client, WriteStream<Buffer> writeStream) {
client
.get(8080, "myserver.mycompany.com", "/some-uri")
.as(BodyCodec.pipe(writeStream))
.send(ar -> {
if (ar.succeeded()) {
HttpResponse<Void> response = ar.result();
System.out.println("Received response with status code" + response.statusCode());
} else {
System.out.println("Something went wrong " + ar.cause().getMessage());
}
});
}
示例13: receiveResponseAndDiscard
import io.vertx.ext.web.codec.BodyCodec; //导入依赖的package包/类
public void receiveResponseAndDiscard(WebClient client) {
client
.get(8080, "myserver.mycompany.com", "/some-uri")
.as(BodyCodec.none())
.send(ar -> {
if (ar.succeeded()) {
HttpResponse<Void> response = ar.result();
System.out.println("Received response with status code" + response.statusCode());
} else {
System.out.println("Something went wrong " + ar.cause().getMessage());
}
});
}
示例14: testResponseBodyAsAsJsonObject
import io.vertx.ext.web.codec.BodyCodec; //导入依赖的package包/类
@Test
public void testResponseBodyAsAsJsonObject() throws Exception {
JsonObject expected = new JsonObject().put("cheese", "Goat Cheese").put("wine", "Condrieu");
server.requestHandler(req -> req.response().end(expected.encode()));
startServer();
HttpRequest<Buffer> get = client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath");
get
.as(BodyCodec.jsonObject())
.send(onSuccess(resp -> {
assertEquals(200, resp.statusCode());
assertEquals(expected, resp.body());
testComplete();
}));
await();
}
示例15: testResponseBodyAsAsJsonMapped
import io.vertx.ext.web.codec.BodyCodec; //导入依赖的package包/类
@Test
public void testResponseBodyAsAsJsonMapped() throws Exception {
JsonObject expected = new JsonObject().put("cheese", "Goat Cheese").put("wine", "Condrieu");
server.requestHandler(req -> req.response().end(expected.encode()));
startServer();
HttpRequest<Buffer> get = client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath");
get
.as(BodyCodec.json(WineAndCheese.class))
.send(onSuccess(resp -> {
assertEquals(200, resp.statusCode());
assertEquals(new WineAndCheese().setCheese("Goat Cheese").setWine("Condrieu"), resp.body());
testComplete();
}));
await();
}