Vert.x WebClient WebClientOptions

Since Vert.x Web Client herein with reference to official documents. Apply the words of the official website,

Vert.x Web Client is an asynchronous HTTP and HTTP / 2 network client.

Relatively speaking, this is a relatively small frame, and the functions are very straightforward, easy to use to do a HTTP client. It has the following features:

Json body encoding / decoding
request parameters
unified error handling
form submission
should be noted that the core package and Vertx HttpClient have a lot of contact. It inherits the HttpClient, provides more functionality.

Reference library
to use the library is very simple. If Maven, add the following dependencies.

<dependency>
<the groupId> io.vertx </ the groupId>
<the artifactId> VertX-Web-Client </ the artifactId>
<Version> 3.4.2 </ Version>
</ dependency>
 
If Gradle, add the following dependencies.

{Dependencies
the compile 'io.vertx: VertX-Web-Client: 3.4.2'
}
 
create client
to create slightly different clients.

WebClient client = WebClient.create (vertx);
 
If you want to add configuration parameters, you can do so.

Options = new new WebClientOptions WebClientOptions ()
.setUserAgent ( "My-the App / 1.2.3");
options.setKeepAlive (to false);
the WebClient Client = WebClient.create (VertX, Options);
 
if already HttpClient, it can be reused.

WebClient client = WebClient.wrap (httpClient); 
initiation request
requesting no request body
which is the simplest case, a general GET, HEAD request, etc. are lost in this way.

webClient.get ( "www.baidu.com", "/")
.send (Ar -> {
IF (ar.succeeded ()) {
the HttpResponse <Buffer> ar.result Response = ();
System.out.println ( response.body ());
} the else {
System.out.println (ar.cause ());
}
});
 
If you want to carry query parameters may be employed streaming API.

Client
.get (8080, "myserver.mycompany.com", "/ some-URI")
.addQueryParam ( "param", "param_value")
.send (Ar -> {});
 
may be provided directly in the URL query parameter.

HttpRequest<Buffer> request = client.get(8080, "myserver.mycompany.com", "/some-uri");

// add the parameter 1
request.addQueryParam ( "param1", "param1_value");

// URL with the overlay parameters
request.uri ( "/ some-uri param1 = param1_value & param2 = param2_value?");
 
Add request body
if using POST method to pass parameters, or upload pictures, would need to request a request with the body. In this case, only the additional use of a method such as a request to add sendXXX can body to be transferred.

webClient.post ( "httpbin.org", "/ POST")
.sendBuffer (Buffer.buffer ( "& name = Yitian Age = 25"), Ar -> {
IF (ar.succeeded ()) {
the HttpResponse <Buffer> Response = ar.result ();
System.out.println (response.body ());
}
});
 
the information to be transmitted if the relatively large, may be used sendStream method, using a stream of data transmitted.

Client
.post (8080, "myserver.mycompany.com", "/ some-URI")
.sendStream (Stream, RESP -> {});
 
Json request body
may sometimes need to send Json data, this method can be used when sendJsonObject .

Client
.post (8080, "myserver.mycompany.com", "/ some-URI")
.sendJsonObject (JSONObject new new ()
.put ( "firstName", "Dale")
.put ( "lastName", "Cooper") , Ar -> {
IF (ar.succeeded ()) {
// Ok
}
});
 
transmitted form
the transmission form used sendForm method.

Multimap = MultiMap.caseInsensitiveMultiMap the MultiMap ();
multiMap.add ( "name", "Yitian");
multiMap.add ( "Age", "25");
webClient.post ( "httpbin.org", "/ POST")
.sendForm (Multimap, Ar -> {
IF (ar.succeeded ()) {
the HttpResponse <Buffer> ar.result Response = ();
System.out.println (response.body ());
}
});
 
default form using application / x-www-form- urlencoded Content type type of transmission may be modified as multipart / form-data type.

Client
.post (8080, "myserver.mycompany.com", "/ some-URI")
.putHeader ( "Content-type", "multipart / form-Data")
.sendForm (form, Ar -> {
IF (Ar .succeeded ()) {
// Ok
}
});
 
currently does not support file upload, upload files will be supported in a future release.

Modification request header
can add and modify the request header to be transmitted.

HttpRequest<Buffer> request = client.get(8080, "myserver.mycompany.com", "/some-uri");
MultiMap headers = request.headers();
headers.set("content-type", "application/json");
headers.set("other-header", "foo");
 
也可以调用putHeader方法直接添加请求头。

The HttpRequest <Buffer> = client.get Request (8080, "myserver.mycompany.com", "/ some-URI");
request.putHeader ( "Content-type", "file application / JSON");
request.putHeader ( " other-header "," foo " );
 
reuse request
if you need to initiate multiple requests for the same address, we can set the next request, then re-use it.

HttpRequest<Buffer> get = client.get(8080, "myserver.mycompany.com", "/some-uri");
get.send(ar -> {
if (ar.succeeded()) {
// Ok
}
});

// reuse
get.send (Ar -> {
IF (ar.succeeded ()) {
// Ok
}
});
  
timeout
when initiating a request timeout value may be set in milliseconds.

Client
.get (8080, "myserver.mycompany.com", "/ some-URI")
.timeout (5000)
.send (Ar -> {
IF (ar.succeeded ()) {
// Ok
} the else {
// When the cause is BE a timeout Might iS java.util.concurrent.TimeoutException
}
});
 
processing request
processing request is asynchronous. Here ar the actual type is the type of AsyncResult, on behalf of the asynchronous result. If the result is successful, the call result () method returns an HttpResponse <Buffer> type of object, this is the result we initiated the request, calling statusCode (), body () methods can view the result.

webClient.get ( "www.baidu.com", "/")
.send (Ar -> {
IF (ar.succeeded ()) {
the HttpResponse <Buffer> ar.result Response = ();
System.out.println ( response.body ());
} the else {
System.out.println (ar.cause ());
}
});
 
decoded response
foregoing Buffer is returned as the response. If we determined that the response is a plain string, Json objects, POJO class can be mapped and Json WriteStream, we can decode response. For example, the following material will be decoded in response to JsonObject object.

webClient.post ( "httpbin.org", "/ POST")
.as (BodyCodec.jsonObject ())
.sendBuffer (Buffer.buffer ( "& name = Yitian Age = 25"), Ar -> {
IF (ar.succeeded ( )) {
the HttpResponse <JSONObject> ar.result response = ();
System.out.println (response.body ());
}
});
 
BodyCodec class there are several methods can be decoded in response to different types of body. If the larger response body, the body may be directly in response to an output stream, after reading slowly.

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 Status Response code with" + Response.StatusCode ());
} the else {
System.out.println ( "Something Wrong Wentworth" + ar.cause () the getMessage ().);
}
}) ;
 
If no response is determined body, none can be used directly () method throw response body.

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 Status Response code with" + Response.StatusCode ());
} the else {
System.out.println ( "Something Wrong Wentworth" + ar.cause () the getMessage ().);
}
}) ;
 
Of course, if the response body or decoded into Buffer, we can still call bodyAsXXX methods to decode the response body. This method is only applicable to the body in response Buffer.

client
.get(8080, "myserver.mycompany.com", "/some-uri")
.send(ar -> {
if (ar.succeeded()) {

HttpResponse<Buffer> response = ar.result();

// Decode the body as a json object
JsonObject body = response.bodyAsJsonObject();

System.out.println ( "Received Status Response code with" + Response.StatusCode () + "body with" + body);
} the else {
System.out.println ( "Something Wrong Wentworth" + ar.cause () the getMessage. ());
}
});
 
using HTTPS
is HTTP Common previously used, if you want to use HTTPS connection is also easy to specify the port number to 443.

client
.get(443, "myserver.mycompany.com", "/some-uri")
.ssl(true)
.send(ar -> {
if (ar.succeeded()) {
// Obtain response
HttpResponse<Buffer> response = ar.result();

System.out.println ( "Received Status Response code with" + Response.StatusCode ());
} the else {
System.out.println ( "Something Wrong Wentworth" + ar.cause () the getMessage ().);
}
}) ; 
or using an absolute path sign URI can.

client
.getAbs("https://myserver.mycompany.com:4043/some-uri")
.send(ar -> {
if (ar.succeeded()) {
// Obtain response
HttpResponse<Buffer> response = ar.result();

System.out.println("Received response with status code" + response.statusCode());
} else {
System.out.println("Something went wrong " + ar.cause().getMessage());
}
});  

Guess you like

Origin www.cnblogs.com/endv/p/12601902.html