WebClient HTTP 请求问题处理模板(泛型响应、忽略 SSL 证书等)

server/2024/12/29 0:51:40/
http://www.w3.org/2000/svg" style="display: none;">

一、请求大数据

1、问题案例
(1)Server
  • 该接口返回一段大小为 1MB 的字符串

    @GetMapping(“/testBigData”)
    public String testBigData() {
    int targetSize = 1024 * 1024; // 1MB
    StringBuilder sb = new StringBuilder(targetSize);
    for (int i = 0; i < targetSize; i++) sb.append(‘A’);
    String largeString = sb.toString();
    return largeString;
    }

(2)Client
WebClient webClient = WebClient.create();Mono<String> response = webClient.get().uri("http://127.0.0.1:9999/test/testBigData").retrieve().bodyToMono(String.class);response.subscribe(result -> {System.out.println("请求成功,结果长度为:" + result.length());
}, throwable -> {throwable.printStackTrace();
});
  • 输出结果,因为数据量超过了设定的最大限制而报错

    org.springframework.web.reactive.function.client.WebClientResponseException: 200 OK from GET http://127.0.0.1:9999/test/testBigData
    Caused by: org.springframework.core.io.buffer.DataBufferLimitException: Exceeded limit on max bytes to buffer : 262144

2、处理策略
  • 调整数据量的最大限制

    int size = 2 * 1024 * 1024;
    ExchangeStrategies strategies = ExchangeStrategies.builder()
    .codecs(codecs -> codecs.defaultCodecs().maxInMemorySize(size))
    .build();

    WebClient webClient = WebClient.builder()
    .exchangeStrategies(strategies)
    .build();

    Mono response = webClient.get()
    .uri(“http://127.0.0.1:9999/test/testBigData”)
    .retrieve()
    .bodyToMono(String.class);

    response.subscribe(result -> {
    System.out.println(“请求成功,结果长度为:” + result.length());
    }, throwable -> {
    throwable.printStackTrace();
    });


二、不使用缓冲区请求图片

1、问题案例
(1)Server
  • application.yaml

    server:
    port: 9999
    spring:
    mvc:
    static-path-pattern: /file/download/**
    web:
    resources:
    static-locations: file:D:/fileUpload/

(2)Client
WebClient webClient = WebClient.create();Mono<byte[]> response = webClient.get().uri("http://127.0.0.1:9999/file/download/1.png").accept(MediaType.IMAGE_JPEG, MediaType.IMAGE_PNG).retrieve().bodyToMono(byte[].class);response.subscribe(result -> {System.out.println("请求成功");if (result != null) System.out.println("result length is " + result.length);
}, throwable -> {throwable.printStackTrace();
});
  • 输出结果,不使用 DataBuffer,会因为数据量超过了设定的最大限制而报错

    org.springframework.web.reactive.function.client.WebClientResponseException: 200 OK from GET http://127.0.0.1:9999/file/download/1.png
    Caused by: org.springframework.core.io.buffer.DataBufferLimitException: Exceeded limit on max bytes to buffer : 262144

2、处理策略
  • 调整数据量的最大限制

    int size = 10 * 1024 * 1024;
    ExchangeStrategies strategies = ExchangeStrategies.builder()
    .codecs(codecs -> codecs.defaultCodecs().maxInMemorySize(size))
    .build();

    WebClient webClient = WebClient.builder()
    .exchangeStrategies(strategies)
    .build();

    Mono<byte[]> response = webClient.get()
    .uri(“http://127.0.0.1:9999/file/download/1.png”)
    .accept(MediaType.IMAGE_JPEG, MediaType.IMAGE_PNG)
    .retrieve()
    .bodyToMono(byte[].class);

    response.subscribe(result -> {
    System.out.println(“请求成功”);
    if (result != null) System.out.println("result length is " + result.length);
    }, throwable -> {
    throwable.printStackTrace();
    });


三、泛型响应

1、问题案例
(1)Entity
  • Person.java

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public class Person {
    private String name;
    private int age;
    private T thing;
    }

  • Computer.java

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public class Computer {
    private String name;
    private Double price;
    }

(2)Server
@GetMapping("/testGeneric")
public Person<Computer> testGeneric() {Computer computer = new Computer("联想电脑", 8999.0);Person<Computer> person = new Person<Computer>("张三", 20, computer);return person;
}
(3)Client
WebClient webClient = WebClient.create();Mono<Person> response = webClient.get().uri("http://127.0.0.1:9999/test/testGeneric").retrieve().bodyToMono(Person.class);response.subscribe(person -> {System.out.println("请求成功");System.out.println(person);Computer computer = (Computer) person.getThing();System.out.println(computer);
}, throwable -> {System.out.println("请求失败,结果为:" + throwable.getMessage());
});
  • 输出结果,对于嵌套的泛型强转失败

    请求成功
    Person(name=张三, age=20, thing={name=联想电脑, price=8999.0})
    请求失败,结果为:class java.util.LinkedHashMap cannot be cast to class com.zy.api.Computer (java.util.LinkedHashMap is in module java.base of loader ‘bootstrap’; com.zy.api.Computer is in unnamed module of loader ‘app’)

2、处理策略
  • 正确指示 bodyToMono 方法

    WebClient webClient = WebClient.create();

    Mono<Person> response = webClient.get()
    .uri(“http://127.0.0.1:9999/test/testGeneric”)
    .retrieve()
    .bodyToMono(new ParameterizedTypeReference<Person>() {
    });

    response.subscribe(person -> {
    System.out.println(“请求成功”);
    System.out.println(person);
    Computer computer = person.getThing();
    System.out.println(computer);
    }, throwable -> {
    System.out.println(“请求失败,结果为:” + throwable.getMessage());
    });


四、跟随重定向响应

1、问题案例
(1)Server
server:port: 9999
spring:mvc:static-path-pattern: /file/download/**web:resources:static-locations: file:D:/fileUpload/@GetMapping("/testRedirect2Image")
public ResponseEntity testRedirect2Image() {return ResponseEntity.status(HttpStatus.SEE_OTHER).header("Location", "http://127.0.0.1:9999/file/download/1.png").build();
}
(2)Client
WebClient webClient = WebClient.create();webClient.get().uri("http://127.0.0.1:9999/test/testRedirect2Image").accept(MediaType.IMAGE_JPEG, MediaType.IMAGE_PNG) // 指定了客户端期望接受的响应内容类型.retrieve() // 获取响应.bodyToFlux(DataBuffer.class) // 将响应体转换为 Flux<DataBuffer>.reduce(new org.apache.tomcat.util.http.fileupload.ByteArrayOutputStream(), (baos, dataBuffer) -> {try {byte[] bytes = new byte[dataBuffer.readableByteCount()];dataBuffer.read(bytes);baos.write(bytes);DataBufferUtils.release(dataBuffer);} catch (IOException e) {e.printStackTrace();DataBufferUtils.release(dataBuffer);return null;}return baos;}).map(byteArrayOutputStream -> {if (byteArrayOutputStream == null) return null;return byteArrayOutputStream.toByteArray();}).subscribe(result -> {if (result == null) {System.out.println("result is null");return;}System.out.println("result length: " + result.length);}, throwable -> {throwable.printStackTrace();});
  • 输出结果,默认不会跟随重定向响应

    result length: 0

2、处理策略
  • 收到开启跟随重定向响应

    HttpClient httpClient = HttpClient.create()
    .followRedirect(true); // 允许跟随重定向

    WebClient webClient = WebClient.builder()
    .clientConnector(new ReactorClientHttpConnector(httpClient))
    .build();

    webClient.get()
    .uri(“http://127.0.0.1:9999/test/testRedirect2Image”)
    .accept(MediaType.IMAGE_JPEG, MediaType.IMAGE_PNG) // 指定了客户端期望接受的响应内容类型
    .retrieve() // 获取响应
    .bodyToFlux(DataBuffer.class) // 将响应体转换为 Flux
    .reduce(new org.apache.tomcat.util.http.fileupload.ByteArrayOutputStream(), (baos, dataBuffer) -> {
    try {
    byte[] bytes = new byte[dataBuffer.readableByteCount()];
    dataBuffer.read(bytes);
    baos.write(bytes);
    DataBufferUtils.release(dataBuffer);
    } catch (IOException e) {
    e.printStackTrace();
    DataBufferUtils.release(dataBuffer);
    return null;
    }
    return baos;
    })
    .map(byteArrayOutputStream -> {
    if (byteArrayOutputStream == null) return null;
    return byteArrayOutputStream.toByteArray();
    })
    .subscribe(result -> {
    if (result == null) {
    System.out.println(“result is null”);
    return;
    }
    System.out.println("result length: " + result.length);
    }, throwable -> {
    throwable.printStackTrace();
    });


五、SSL 证书

1、问题案例
WebClient webClient = WebClient.create();Mono<byte[]> response = webClient.get().uri(url).accept(MediaType.IMAGE_JPEG, MediaType.IMAGE_PNG).retrieve().bodyToFlux(DataBuffer.class).reduce(new ByteArrayOutputStream(), (baos, dataBuffer) -> {try {byte[] bytes = new byte[dataBuffer.readableByteCount()];dataBuffer.read(bytes);baos.write(bytes);DataBufferUtils.release(dataBuffer);} catch (IOException e) {e.printStackTrace();DataBufferUtils.release(dataBuffer);return null;}return baos;}).map(byteArrayOutputStream -> {if (byteArrayOutputStream == null) return null;return byteArrayOutputStream.toByteArray();});response.subscribe(result -> {System.out.println("请求成功");if (result != null) System.out.println("result length is " + result.length);
}, throwable -> {throwable.printStackTrace();
});
  • 输出结果

    org.springframework.web.reactive.function.client.WebClientRequestException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

2、忽略 SSL 证书
SslContext context = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build();HttpClient httpClient = HttpClient.create().secure(t -> t.sslContext(context));WebClient webClient = WebClient.builder().clientConnector(new ReactorClientHttpConnector(httpClient)).build();Mono<byte[]> response = webClient.get().uri(url).accept(MediaType.IMAGE_JPEG, MediaType.IMAGE_PNG).retrieve().bodyToFlux(DataBuffer.class).reduce(new ByteArrayOutputStream(), (baos, dataBuffer) -> {try {byte[] bytes = new byte[dataBuffer.readableByteCount()];dataBuffer.read(bytes);baos.write(bytes);DataBufferUtils.release(dataBuffer);} catch (IOException e) {e.printStackTrace();DataBufferUtils.release(dataBuffer);return null;}return baos;}).map(byteArrayOutputStream -> {if (byteArrayOutputStream == null) return null;return byteArrayOutputStream.toByteArray();});response.subscribe(result -> {System.out.println("请求成功");if (result != null) System.out.println("result length is " + result.length);
}, throwable -> {throwable.printStackTrace();
});

http://www.ppmy.cn/server/154060.html

相关文章

MySQL数据库——数据类型,索引,事务,优化

目录 数据类型 存储MD5用varchar还是char 用varchar存储一本小说 datetime,timstamp myisam和innodb有什么区别 索引 MySQL索引的优缺点 索引失效的原因 聚集索引和非聚集索引 MySQL索引采用B树 B,B树 为什么SQL语句命中索引比不命中快 事务 事务的隔离级别 实现…

Django 中的 reverse 【反向/逆转/扭转/逆向】使用详解以及使用案例

Django 中的 reverse 使用详解 reverse &#xff1a;中文翻译:反向/逆转/扭转/逆向 什么是 reverse 方法&#xff1f; Django 中的 reverse 方法用于根据视图的名称和传递的参数&#xff0c;动态生成URL。这样做的好处是&#xff0c;如果你在 urls.py 中更改了URL模式&#x…

【Java基础面试题038】栈和队列在Java中的区别是什么?

回答重点 栈&#xff08;Stack&#xff09;&#xff1a;遵循后进先出&#xff08;LIFO&#xff0c;Last In&#xff0c;First Out&#xff09;原则。即&#xff0c;最后插入的元素最先被移除。主要操作包括push&#xff08;入栈&#xff09;和pop&#xff08;出栈&#xff09;…

QTDemo:串口调试工具

项目简介 本项目通过QT框架设计一款可以在Windows、Linux等平台的跨平台串口助手&#xff0c;串口功能能够满足基本的调试需求。 本项目采用的版本为&#xff1a;QT5.14 visual studio 2022 进行开发。 项目源码&#xff1a;https://github.com/say-Hai/MyCOMDemo 项目页面&am…

区块链平台安全属性解释

区块链平台安全属性解释 双向认证 解释:双向认证是指在通信过程中,**通信双方都需要对对方的身份进行验证,确保对方是合法的、可信任的实体。**只有双方身份都得到确认后,通信才会被允许进行,从而防止非法用户的接入和数据的窃取或篡改。举例:在基于区块链和联邦学习的数…

【ELK】filebeat采集数据输出到kafka指定topic

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 背景filebeat主体配置filebeat.inputs部分filebeat.output部分 filebeat完整配置 背景 今天收到需求&#xff0c;生产环境中通需要优化filebeat的输出&#xff0c;…

StarRocks 存算分离在得物的降本增效实践

编者荐语&#xff1a; 得物优化数据引擎布局&#xff0c;近期将 4000 核 ClickHouse 迁移至自建 StarRocks&#xff0c;成本降低 40%&#xff0c;查询耗时减半&#xff0c;集群稳定性显著提升。本文详解迁移实践与成果&#xff0c;文末附丁凯剑老师 StarRocks Summit Asia 2024…

设计一个监控摄像头物联网IOT(webRTC、音视频、文件存储)

前言&#xff1a; 设计一个完整的 监控摄像头物联网 IoT 平台 涉及 视频直播和点播、WebRTC 和 文件存储模块&#xff0c;可以分为以下几个主要部分&#xff1a;摄像头设备、服务端处理、Web 前端、视频流存储和回放。以下是结合这些技术的一个具体完整流程设计&#xff0c;涵盖…