像 @FeignClient
和 @RetrofitClient
那样使用 Spring Boot 3.x
中的 HTTP Interface
。
<dependency>
<groupId>io.github.llnancy</groupId>
<artifactId>httpexchange-spring-boot-starter</artifactId>
<version>0.0.2</version>
</dependency>
接口必须使用 @io.github.llnancy.httpexchange.core.ExchangeClient
注解标记!HTTP
相关注解可参考官方文档:Spring 官方文档。
@ExchangeClient(baseUrl = "${test.baseUrl}")
public interface HttpApi {
@PostExchange("/post")
Mono<Map<String, Object>> post(@RequestBody Map<String, Object> params);
}
将接口注入到其它 Service
中即可使用!
@Service
public class TestService {
@Autowired
private HttpApi httpApi;
public void test() {
// 使用 httpApi 发起 HTTP 请求
}
}
报错 Caused by: org.springframework.core.io.buffer.DataBufferLimitException: Exceeded limit on max bytes to buffer : 262144
Caused by: org.springframework.core.io.buffer.DataBufferLimitException: Exceeded limit on max bytes to buffer : 262144
at org.springframework.core.io.buffer.LimitedDataBufferList.raiseLimitException(LimitedDataBufferList.java:99)
具体参考 https://www.baeldung.com/spring-webflux-databufferlimitexception
本框架支持的方式如下:
-
自定义一个
io.github.llnancy.httpexchange.core.ClientCodecConfigurerConsumer
@Component class MyClientCodecConfigurerConsumer implements ClientCodecConfigurerConsumer { @Override public Consumer<ClientCodecConfigurer> consumer() { return configurer -> configurer.defaultCodecs() .maxInMemorySize(500 * 1024); } }
-
在
@ExchangeClient
注解中指定codecConfigurerConsumer
@ExchangeClient(baseUrl = "https://lilu.org.cn", codecConfigurerConsumer = MyClientCodecConfigurerConsumer.class) public interface MyExchangeClient { }
支持。
Copyright (c) 2023-present llnancy