网站开发项目流程设计,网站开发的教学视频,不同端口装 wordpress,网站设计制作系统哪个好#x1f4bb;目录 1、介绍2、使用2.1、添加配置2.1.1、依赖2.1.2、工具类2.1.3、实体2.1.4、Controller接口 2.2、Apache HttpClient使用2.3 、OkHttp使用2.4、WebClient使用 1、介绍
现在java使用的http客户端主要包括以下几种 而这些中使用得最频繁的主要是#xff1a; A… 目录 1、介绍2、使用2.1、添加配置2.1.1、依赖2.1.2、工具类2.1.3、实体2.1.4、Controller接口 2.2、Apache HttpClient使用2.3 、OkHttp使用2.4、WebClient使用 1、介绍
现在java使用的http客户端主要包括以下几种 而这些中使用得最频繁的主要是 Apache HttpClient这是一个功能强大且广泛使用的第三方库用于进行HTTP通讯。它提供了更高级的API和更丰富的功能比如支持连接池、认证、重定向、Cookie管理等。Apache HttpClient可以作为独立的库使用也可以作为Apache HttpComponents项目的一部分。 OkHttp这是另一个流行的第三方库用于进行HTTP通讯。OkHttp提供了简洁的API和高性能的特性支持同步和异步请求以及连接池、缓存、拦截器等功能。OkHttp也是Square公司的一个开源项目。 Spring WebClient这是Spring框架中的一个模块是RestTemplate的升级版用于进行非阻塞的HTTP通讯。它基于Reactive Streams编程模型适用于构建响应式的应用程序。Spring WebClient提供了简单的API来发送HTTP请求和处理响应可以与Spring WebFlux等模块一起使用。
2、使用
下面展示了Apache HttpClient和OkHttp以及Spring WebClient的常用使用方式包括get和post
2.1、添加配置
2.1.1、依赖 !-- https://mvnrepository.com/artifact/org.apache.httpcomponents.client5/httpclient5 --dependencygroupIdorg.apache.httpcomponents.client5/groupIdartifactIdhttpclient5/artifactId/dependency!--okhttp--dependencygroupIdcom.squareup.okhttp3/groupIdartifactIdokhttp/artifactId/dependencydependencygroupIdcn.hutool/groupIdartifactIdhutool-all/artifactIdversion5.8.22/version/dependency!--webClient--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-webflux/artifactId/dependency2.1.2、工具类
Result统一返回类
Data
public class ResultT {//状态码private Integer code;//信息private String message;//数据private T data;private Result(){}//设置数据,返回对象的方法public static T ResultT build(T data, ResultCodeEnum resultCodeEnum) {//创建Result对象设置值返回对象ResultT result new Result();//判断返回结果中是否需要数据if (data ! null) {//设置数据到result对象result.setData(data);}//设置其他值result.setCode(resultCodeEnum.getCode());result.setMessage(resultCodeEnum.getMessage());//返回设置值之后的对象return result;}//成功的方法public static T ResultT ok(T data) {return build(data, ResultCodeEnum.SUCCESS);}//成功的方法public static T ResultT ok() {return build(null, ResultCodeEnum.SUCCESS);}//失败的方法public static T ResultT fail(T data) {return build(data, ResultCodeEnum.FAIL);}
}
ResultCodeEnum返回结果
Getter
public enum ResultCodeEnum {SUCCESS(200,成功),FAIL(201, 失败),;private Integer code;private String message;ResultCodeEnum(Integer code, String message) {this.code code;this.message message;}
}
2.1.3、实体
User
Data
public class User {private String name;private Integer age;private Integer post;
}ProductInfo返回数据
Data
AllArgsConstructor
NoArgsConstructor
ToString
public class ProductInfo implements Serializable {private Long id;/**销量*/private Integer sale;/**价格*/private Integer price;/**名称*/private String name;/**类型*/private Integer type;/**端口*/private Integer port;
}2.1.4、Controller接口
RestController
RequestMapping(/productInfo/index)
public class ProductInfoController {Resourceprivate ProductInfoService productInfoService;// 获取当前服务的端口Value(${server.port})private Integer port;GetMapping(/get)public Result? get(Integer type){ListProductInfo productInfoList getProductInfoList(type);System.out.println(productInfoList);return Result.ok(productInfoList);}PostMapping(/post)public Result? post(RequestBody User user){Integer post user.getPost();ListProductInfo productInfoList getProductInfoList(post);System.out.println(productInfoList);return Result.ok(productInfoList);}public ListProductInfo getProductInfoList(Integer type) {ProductInfo productInfo3 new ProductInfo(3l,800,20,哈密瓜1,1,port);ProductInfo productInfo1 new ProductInfo(1l,50,8,苹果1,1,port);ProductInfo productInfo2 new ProductInfo(2l,200,13,牛肉1,2,port);ProductInfo productInfo4 new ProductInfo(4l,50,9,青菜1,2,port);/* 实际项目中会从数据库查出数据 */ListProductInfo productInfoList Arrays.asList(productInfo1,productInfo2,productInfo3,productInfo4);// 根据传入的类型返回指定类型ListProductInfo result productInfoList.stream().filter(productInfo - productInfo.getType() type).collect(Collectors.toList());return result;}
}
2.2、Apache HttpClient使用
get使用 默认只能同步异步需要加额外的依赖get请求不能携带参数只能通过拼接路径 !--异步请求--dependencygroupIdorg.apache.httpcomponents/groupIdartifactIdhttpasyncclient/artifactId/dependencypublic static void get(CloseableHttpClient httpClient,String url){
// 连接对象try {
// HttpGet不能携带参数如果需要参数只能通过拼接HttpGet httpGet new HttpGet(url?type1);String execute httpClient.execute(httpGet, response - {JSONObject entries new JSONObject(EntityUtils.toString(response.getEntity()));Result result JSONUtil.toBean(entries, Result.class);if (result.getCode() 200) {String data result.getData().toString();System.out.println(data);return data;} else {System.err.println(result.getMessage());return result.getMessage();}});System.out.println(get成功结果execute);} catch (Exception e) {e.printStackTrace();}}post post请求通过setEntity()给HttpPost对象添加请求对象 public static void post(CloseableHttpClient httpClient,String url){HttpPost httpPost new HttpPost(url);User user new User();user.setPost(1);
// 添加参数对象httpPost.setEntity(new StringEntity(JSONUtil.toJsonStr(user)));// 设置请求头httpPost.setHeader(HttpHeaders.CONTENT_TYPE, application/json);try {String execute httpClient.execute(httpPost, response - {JSONObject entries new JSONObject(EntityUtils.toString(response.getEntity()));Result result JSONUtil.toBean(entries, Result.class);if (result.getCode() 200) {String data result.getData().toString();log.info(data);return data;} else {log.error(result.getMessage());return result.getMessage();}});log.info(HttpClient的post成功结果execute);} catch (IOException e) {e.printStackTrace();}}全部代码 执行步骤 创建CloseableHttpClient对象创建get获取post请求对象通过execute()方法发送请求通过response.getEntity()获取响应回来的数据 Slf4j
public class HttpClientUtil {private static String postUrl http://localhost:8202/productInfo/index/post;private static String getUrl http://localhost:8202/productInfo/index/get;public static void main(String[] args) {try {
// 创建客户端对象CloseableHttpClient client HttpClients.createDefault();get(client,getUrl);post(client,postUrl);client.close();} catch (IOException e) {e.printStackTrace();}}public static void get(CloseableHttpClient httpClient,String url){
// 连接对象try {
// HttpGet不能携带参数如果需要参数只能通过拼接HttpGet httpGet new HttpGet(url?type1);String execute httpClient.execute(httpGet, response - {JSONObject entries new JSONObject(EntityUtils.toString(response.getEntity()));Result result JSONUtil.toBean(entries, Result.class);if (result.getCode() 200) {String data result.getData().toString();log.info(data);return data;} else {log.info(result.getMessage());return result.getMessage();}});log.info(HttpClient的get成功结果execute);} catch (Exception e) {e.printStackTrace();}}public static void post(CloseableHttpClient httpClient,String url){HttpPost httpPost new HttpPost(url);User user new User();user.setPost(1);
// 添加参数对象httpPost.setEntity(new StringEntity(JSONUtil.toJsonStr(user)));// 设置请求头httpPost.setHeader(HttpHeaders.CONTENT_TYPE, application/json);try {String execute httpClient.execute(httpPost, response - {JSONObject entries new JSONObject(EntityUtils.toString(response.getEntity()));Result result JSONUtil.toBean(entries, Result.class);if (result.getCode() 200) {String data result.getData().toString();log.info(data);return data;} else {log.error(result.getMessage());return result.getMessage();}});log.info(HttpClient的post成功结果execute);} catch (IOException e) {e.printStackTrace();}}
}2.3 、OkHttp使用 执行步骤 创建OkHttpClient对象用于配置和管理HTTP请求的行为创建Request对象用于描述要发送的HTTP请求。Request对象包含了URL、请求方法、请求头、请求体等信息。创建Call对象使用OkHttpClient的newCall()方法传入Request对象创建一个Call对象Call对象表示一次HTTP请求的调用可以用于执行同步或异步的请求。执行请求同步通过Call对象的execute()方法来异步通过Call对象的enqueue()方法来执行请求并通过回调函数处理响应解析响应通过response.body()得到响应的内容在通过json解析。 Slf4j
public class OkHttpUtil {private static String postUrl http://localhost:8202/productInfo/index/post;private static String getUrl http://localhost:8202/productInfo/index/get;public static void main(String[] args) {OkHttpClient client new OkHttpClient().newBuilder().connectTimeout(30, TimeUnit.SECONDS) //连接超时时间.writeTimeout(30,TimeUnit.SECONDS) //请求超时时间.readTimeout(30,TimeUnit.SECONDS) //响应超时时间.build();
// get(client);
// asyncGet(client);
// post(client);asyncPost(client);}/*** 同步get* param client * return void*/public static void get(OkHttpClient client){
// 创建get请求对象Request request new Request.Builder().url(getUrl?type1).get().header(Content-Type, application/json) // 设置Content-Type请求头.build();try {
// 返回响应对象Response response client.newCall(request).execute();
// 验证响应是否成功if (!response.isSuccessful()) throw new IOException(Unexpected code response);
// 解码对象Result result JSONUtil.toBean(response.body().string(), Result.class);Object data result.getData();System.out.println(data);} catch (IOException e) {e.printStackTrace();}}/*** 异步get* param client* return void*/public static void asyncGet(OkHttpClient client){// 创建get请求对象Request request new Request.Builder().url(getUrl?type1).get() //不指定请求方式默认是get.build();
// 异步发送请求没有返回结果client.newCall(request).enqueue(new Callback() {// 处理失败请求Overridepublic void onFailure(Call call, IOException e) {log.debug(Unexpected code e.getMessage());e.printStackTrace();}// 处理成功请求Overridepublic void onResponse(Call call, Response response) throws IOException {
// 验证响应是否成功if (!response.isSuccessful()) throw new IOException(Unexpected code response);
// 解码对象Result result JSONUtil.toBean(response.body().string(), Result.class);Object data result.getData();System.out.println(data);}});}/*** 同步post* param client* return void*/public static void post(OkHttpClient client){User user new User();user.setPost(1);String str JSONUtil.toJsonStr(user);Request request new Request.Builder().url(postUrl).post(RequestBody.create(MediaType.parse(ContentType.JSON.getValue()), str)).build();Call call client.newCall(request);try {Response response call.execute();// 验证响应是否成功if (!response.isSuccessful()) throw new IOException(Unexpected code response);
// 解码对象Result result JSONUtil.toBean(response.body().string(), Result.class);Object data result.getData();log.info(post请求成功返回结果:{},data);} catch (IOException e) {e.printStackTrace();}}/*** 异步post请求* param client * return void*/public static void asyncPost(OkHttpClient client){User user new User();user.setPost(1);
// 把对象转为json字符串String str JSONUtil.toJsonStr(user);Request request new Request.Builder().url(postUrl).post(RequestBody.create(MediaType.parse(ContentType.JSON.getValue()), str)).build();Call call client.newCall(request);
// 异步请求没返回call.enqueue(new Callback() {
// 请求异常Overridepublic void onFailure(Call call, IOException e) {log.debug(Unexpected code e.getMessage());e.printStackTrace();}
// 请求成功Overridepublic void onResponse(Call call, Response response) throws IOException {// 验证响应是否成功if (!response.isSuccessful()) throw new IOException(Unexpected code response);
// 解码对象Result result JSONUtil.toBean(response.body().string(), Result.class);Object data result.getData();log.info(异步post请求成功返回结果:{},data);}});}
}2.4、WebClient使用
执行步骤 创建WebClient对象推荐使用WebClient.builder()的方式创建因为可以自定义配置客户端的参数直接new是只能使用默认的配置。 构建请求直接通过webClient对象如get()、post()等构建一个请求如果没有指定请求方式默认是get请求。 处理响应使用响应对象如Mono、Flux等来处理响应数据。你可以通过操作符如map()、flatMap()等对响应进行转换、过滤、合并等操作。 订阅响应使用subscribe()方法来订阅响应流启动请求并处理响应数据。你可以通过回调函数或操作符链来处理响应数据。
Component
Slf4j
public class WebClientUtil {private static String postUrl http://localhost:8202/productInfo/index/post;private static String getUrl http://localhost:8202/productInfo/index/get;/*** 同步执行get请求* param webClient* return void*/public void get(WebClient webClient){MonoResult mono webClient.get().uri(getUrl ?type2).retrieve().bodyToMono(Result.class);// 获取返回结果 block()会进行堵塞等待返回结果Result result mono.block();if (result.getCode()200){log.info(get请求返回结果{},result.getData());}}/*** 异步get* return void*/public void asyncGet(){HttpClient client HttpClient.create().option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000) //设置连接超时.doOnConnected(conn - conn.addHandler(new ReadTimeoutHandler(10, TimeUnit.SECONDS)) //写入超时.addHandler(new WriteTimeoutHandler(10))); //读取超时// 也可以以这种方式创建WebClient可以添加请求头和url以及一些参数WebClient webClient WebClient.builder().clientConnector(new ReactorClientHttpConnector(client)).baseUrl(getUrl).defaultHeader(HttpHeaders.USER_AGENT, Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko))
// .defaultCookie() //添加请求Cookie.build();// 获取返回结果MonoResult mono webClient.get().uri( ?type2)// 请求路径会拼接上面的.accept(MediaType.APPLICATION_JSON)// .retrieve() //获取响应体
// .bodyToMono(Result.class); //指定获取的类型,直接获取结果。// 或者通过该方式手动处理结果.exchangeToMono(response-{if (response.statusCode().equals(HttpStatus.OK)) {
// 成功返回return response.bodyToMono(Result.class);}else {
// 失败返回return response.createException().flatMap(Mono::error);}});// 异步获取返回结果mono.subscribe(result - {if (result.getCode() 200) {log.info(get请求返回结果{}, result.getData());}});System.out.println(执行完成);}/*** 同步post* return void*/public void post(){// 创建 WebClient 对象WebClient webClient WebClient.builder().baseUrl(getUrl).build();User user new User();user.setPost(2);MonoResult mono webClient.post().uri(postUrl).contentType(MediaType.APPLICATION_JSON).header(token,12321412).body(BodyInserters.fromValue(user)).retrieve().bodyToMono(Result.class);Result result mono.block();if (result.getCode()200){log.info(post请求返回结果{},result.getData());}}/*** WebClient异步请求* return void*/public void asyncPost(){// 创建 WebClient 对象WebClient webClient WebClient.builder().baseUrl(getUrl).build();User user new User();user.setPost(2);MonoResult mono webClient.post().uri(postUrl).contentType(MediaType.APPLICATION_JSON) //指定类型.header(token,12321412) //添加请求头.body(BodyInserters.fromValue(user)) //添加请求对象.retrieve().bodyToMono(Result.class);
// 异步请求mono.subscribe(result - {if (result.getCode()200){log.info(post异步请求返回结果{},result.getData());}});}
}