当前位置: 首页 > wzjs >正文

电商网站搭建流程辽宁好的百度seo公司

电商网站搭建流程,辽宁好的百度seo公司,威海市住房和城乡建设局官方网站,国家精品课程网官网在 Java 开发中,HTTP 客户端是与服务端交互的关键组件。随着技术发展,出现了多种 HTTP 客户端库,本文汇总了常用的 Java HTTP 客户端,介绍其特点、适用场景,并附上简单使用示例,方便开发者快速选择和上手。…

在 Java 开发中,HTTP 客户端是与服务端交互的关键组件。随着技术发展,出现了多种 HTTP 客户端库,本文汇总了常用的 Java HTTP 客户端,介绍其特点、适用场景,并附上简单使用示例,方便开发者快速选择和上手。


1.常用 HTTP 客户端一览

名称简介特点
HttpClient(JDK 自带)Java 11 引入的官方 HTTP 客户端支持异步、HTTP/2,API 现代
RestTemplateSpring 提供的同步 HTTP 客户端(即将被淘汰)简洁,适合小项目;被 WebClient 替代
WebClientSpring 5 提供的响应式 HTTP 客户端支持响应式编程,推荐替代 RestTemplate
OkHttpClientSquare 出品,轻量级高性能 HTTP 客户端支持异步、连接池、拦截器
Apache HttpClientApache 出品的经典 HTTP 客户端库功能强大、稳定、支持各种高级特性
Feign声明式 HTTP 客户端,适用于 Spring Cloud开发效率高,集成 Ribbon/Hystrix
RetrofitSquare 出品,封装 OkHttp 的声明式客户端强大的 JSON 自动转换,适合 REST API
AsyncHttpClient异步非阻塞 HTTP 客户端(如 Netty 实现)高并发性能好,用于微服务场景
Vert.x WebClientVert.x 框架的异步 HTTP 客户端轻量级、响应式,非常适合微服务
Jetty HttpClientJetty 提供的 HTTP 客户端实现支持异步和 HTTP/2,常用于嵌入式


2.推荐选择场景

需求推荐客户端
简单同步请求RestTemplate(传统)或 HttpClient(Java 11+)
响应式或高并发WebClient, AsyncHttpClient, Vert.x WebClient
简洁封装、声明式调用Feign, Retrofit
高度可定制OkHttp, Apache HttpClient

3.各客户端 GET 和 POST 示例


1. HttpClient(Java 11+)

// GET 请求
HttpClient client = HttpClient.newHttpClient();HttpRequest getRequest = HttpRequest.newBuilder().uri(URI.create("https://jsonplaceholder.typicode.com/posts/1")).GET().build();HttpResponse<String> getResponse = client.send(getRequest, HttpResponse.BodyHandlers.ofString());
System.out.println("GET response: " + getResponse.body());// POST 请求
String json = "{\"title\":\"foo\",\"body\":\"bar\",\"userId\":1}";HttpRequest postRequest = HttpRequest.newBuilder().uri(URI.create("https://jsonplaceholder.typicode.com/posts")).header("Content-Type", "application/json").POST(HttpRequest.BodyPublishers.ofString(json)).build();HttpResponse<String> postResponse = client.send(postRequest, HttpResponse.BodyHandlers.ofString());
System.out.println("POST response: " + postResponse.body());

2. RestTemplate

RestTemplate restTemplate = new RestTemplate();// GET 请求
String getResult = restTemplate.getForObject("https://jsonplaceholder.typicode.com/posts/1", String.class);
System.out.println("GET response: " + getResult);// POST 请求
String url = "https://jsonplaceholder.typicode.com/posts";
Map<String, Object> postBody = new HashMap<>();
postBody.put("title", "foo");
postBody.put("body", "bar");
postBody.put("userId", 1);String postResult = restTemplate.postForObject(url, postBody, String.class);
System.out.println("POST response: " + postResult);

3. WebClient

WebClient client = WebClient.create();// GET 请求
client.get().uri("https://jsonplaceholder.typicode.com/posts/1").retrieve().bodyToMono(String.class).subscribe(response -> System.out.println("GET response: " + response));// POST 请求
String json = "{\"title\":\"foo\",\"body\":\"bar\",\"userId\":1}";client.post().uri("https://jsonplaceholder.typicode.com/posts").header("Content-Type", "application/json").bodyValue(json).retrieve().bodyToMono(String.class).subscribe(response -> System.out.println("POST response: " + response));

4. OkHttpClient

OkHttpClient client = new OkHttpClient();// GET 请求
Request getRequest = new Request.Builder().url("https://jsonplaceholder.typicode.com/posts/1").build();Response getResponse = client.newCall(getRequest).execute();
System.out.println("GET response: " + getResponse.body().string());// POST 请求
MediaType JSON = MediaType.get("application/json; charset=utf-8");
String json = "{\"title\":\"foo\",\"body\":\"bar\",\"userId\":1}";RequestBody body = RequestBody.create(json, JSON);
Request postRequest = new Request.Builder().url("https://jsonplaceholder.typicode.com/posts").post(body).build();Response postResponse = client.newCall(postRequest).execute();
System.out.println("POST response: " + postResponse.body().string());

5. Apache HttpClient

CloseableHttpClient client = HttpClients.createDefault();// GET 请求
HttpGet getRequest = new HttpGet("https://jsonplaceholder.typicode.com/posts/1");
CloseableHttpResponse getResponse = client.execute(getRequest);
System.out.println("GET response: " + EntityUtils.toString(getResponse.getEntity()));// POST 请求
HttpPost postRequest = new HttpPost("https://jsonplaceholder.typicode.com/posts");
postRequest.setHeader("Content-Type", "application/json");
StringEntity entity = new StringEntity("{\"title\":\"foo\",\"body\":\"bar\",\"userId\":1}");
postRequest.setEntity(entity);CloseableHttpResponse postResponse = client.execute(postRequest);
System.out.println("POST response: " + EntityUtils.toString(postResponse.getEntity()));

6. Feign

interface JsonPlaceholderClient {@RequestLine("GET /posts/1")String getPost();@RequestLine("POST /posts")@Headers("Content-Type: application/json")String createPost(String json);
}JsonPlaceholderClient client = Feign.builder().target(JsonPlaceholderClient.class, "https://jsonplaceholder.typicode.com");// GET 请求
String getResponse = client.getPost();
System.out.println("GET response: " + getResponse);// POST 请求
String json = "{\"title\":\"foo\",\"body\":\"bar\",\"userId\":1}";
String postResponse = client.createPost(json);
System.out.println("POST response: " + postResponse);

7. Retrofit

public interface ApiService {@GET("posts/1")Call<Post> getPost();@POST("posts")Call<Post> createPost(@Body Post post);
}Retrofit retrofit = new Retrofit.Builder().baseUrl("https://jsonplaceholder.typicode.com/").addConverterFactory(GsonConverterFactory.create()).build();ApiService service = retrofit.create(ApiService.class);// GET 请求
service.getPost().enqueue(new Callback<Post>() {public void onResponse(Call<Post> call, Response<Post> response) {System.out.println("GET response: " + response.body());}public void onFailure(Call<Post> call, Throwable t) {t.printStackTrace();}
});// POST 请求
Post newPost = new Post("foo", "bar", 1);
service.createPost(newPost).enqueue(new Callback<Post>() {public void onResponse(Call<Post> call, Response<Post> response) {System.out.println("POST response: " + response.body());}public void onFailure(Call<Post> call, Throwable t) {t.printStackTrace();}
});

8. AsyncHttpClient

AsyncHttpClient client = Dsl.asyncHttpClient();// GET 请求
client.prepareGet("https://jsonplaceholder.typicode.com/posts/1").execute().toCompletableFuture().thenAccept(response -> System.out.println("GET response: " + response.getResponseBody())).join();// POST 请求
String json = "{\"title\":\"foo\",\"body\":\"bar\",\"userId\":1}";client.preparePost("https://jsonplaceholder.typicode.com/posts").setHeader("Content-Type", "application/json").setBody(json).execute().toCompletableFuture().thenAccept(response -> System.out.println("POST response: " + response.getResponseBody())).join();

9. Vert.x WebClient

Vert.x 是一个轻量级、响应式的应用框架,其 WebClient 是异步非阻塞的 HTTP 客户端,非常适合高并发和微服务场景。

Vertx vertx = Vertx.vertx();
WebClient client = WebClient.create(vertx);// GET 请求
client.getAbs("https://jsonplaceholder.typicode.com/posts/1").send(ar -> {if (ar.succeeded()) {System.out.println("GET response: " + ar.result().bodyAsString());} else {ar.cause().printStackTrace();}});// POST 请求
JsonObject json = new JsonObject().put("title", "foo").put("body", "bar").put("userId", 1);client.postAbs("https://jsonplaceholder.typicode.com/posts").putHeader("Content-Type", "application/json").sendJsonObject(json, ar -> {if (ar.succeeded()) {System.out.println("POST response: " + ar.result().bodyAsString());} else {ar.cause().printStackTrace();}});

10. Jetty HttpClient

Jetty 提供的 HTTP 客户端,支持异步和 HTTP/2,适合嵌入式和高性能场景。

HttpClient client = new HttpClient();
client.start();// GET 请求
ContentResponse getResponse = client.GET("https://jsonplaceholder.typicode.com/posts/1");
System.out.println("GET response: " + getResponse.getContentAsString());// POST 请求
Request postRequest = client.POST("https://jsonplaceholder.typicode.com/posts");
postRequest.header("Content-Type", "application/json");
postRequest.content(new StringContentProvider("{\"title\":\"foo\",\"body\":\"bar\",\"userId\":1}"));ContentResponse postResponse = postRequest.send();
System.out.println("POST response: " + postResponse.getContentAsString());client.stop();

4.结语

随着 Java 生态的发展,HTTP 客户端的选择也愈发丰富和多样。无论是 JDK 自带的现代 HttpClient,还是功能强大的第三方库如 OkHttp、Apache HttpClient,以及适合响应式编程的 WebClient,都各有优势,满足不同场景需求。

选择合适的 HTTP 客户端,能极大提升开发效率和应用性能。希望本文的汇总与示例,能帮助你快速掌握各类客户端的基本用法,并根据项目需求做出最佳选择。

如果你有更多使用心得或想了解更深入的高级功能,欢迎留言交流,我们一起成长!

http://www.dtcms.com/wzjs/385676.html

相关文章:

  • 金藏源电商网站建设哪家好省委副书记
  • 外贸网站如何做常用的关键词优化策略有哪些
  • 网站说服力营销型网站策划种子搜索器
  • 用divid做网站代码在线crm
  • 营销神器官方网站营业推广的目标通常是
  • 网站域名怎么用百度知道网页版地址
  • 学做缝纫的网站移动端seo关键词优化
  • 比较好的外贸网站网络推广软文怎么写
  • 建湖做网站的西安seo霸屏
  • 邵阳网站建设上科互联百度推广登录入口官网
  • 购物网站开发案例教程西安网站制作
  • 温州哪里有做网站的国际新闻头条今日国际大事
  • 无锡网站建设解决方案长沙百度推广排名
  • 培训网站完整页面国外广告联盟平台
  • 手机有办法做网站吗济宁百度推广公司有几家
  • 网站页面切换效果找客户的十大方法
  • 湖北去哪做经营性网站备案优化营商环境发言稿
  • 乐清人才网seo优化技术招聘
  • 做兼职网站百度手游排行榜
  • 各大网站每日热点汇总互联网推广怎么找客户
  • .耐思尼克官方网站班级优化大师下载安装
  • 韩国网站模板百度网站打开
  • 仿做唯品会网站网站注册要多少钱
  • 自己用dw做网站要多久如何做好品牌宣传
  • 雄县没有做网站的公司seo营销怎么做
  • 做外贸的有哪些网站有哪些seo电商运营是什么意思
  • 济宁城乡建设局网站厦门百度推广怎么做
  • 做网站赚钱流量友情链接赚钱
  • 旅游订票网站开发谷歌怎么投放广告
  • 电子商务网站开发实务石道元百度竞价托管公司