vertx简单客户端创建
import java.util.HashMap; import java.util.Map; import com.yunva.vertx.test.vertproject.util.JsonUtil; import io.vertx.core.http.HttpClientOptions; import io.vertx.rxjava.core.Vertx; import io.vertx.rxjava.core.http.HttpClient; import io.vertx.rxjava.core.http.HttpClientRequest; public class TestClient { private Vertx vertx = Vertx.vertx(); public HttpClient getClient (){ HttpClientOptions options = new HttpClientOptions(); options.setDefaultPort(8080).setDefaultHost("127.0.0.1").setKeepAlive(false); HttpClient client = vertx.createHttpClient(options); return client; } public static void main(String[] args){ TestClient tct = new TestClient(); HttpClient client = tct.getClient(); Map<String, Object> map = new HashMap<>(); map.put("name", "shad"); map.put("age", 55); map.put("school", "xinhuazhongxue"); String body = JsonUtil.objectToJson(map); client.post("some-uri", response -> {// //do something System.out.println("Received response with status code " + response.statusCode()); response.handler(buffer -> {//读取返回内容 System.out.println(buffer.toString()); }); }). putHeader("content-length", "1000").//请求头 putHeader("content-type", "text/plain"). setTimeout(2000).//请求超时 write(body).//请求内容 end(); /*HttpClientRequest request = client.post("/some-url"); request.handler(response -> {//请求处理 }); request.exceptionHandler(err -> {//请求异常处理 System.out.println("Received response with status code " + err.getMessage()); err.printStackTrace(); });*/ } }