在Java+;中向远程地址发送http请求并返回响应的方法的数量;弹簧靴
我对将请求发送到远程地址,然后使用Java语言在Springboot中返回该响应的可能方式有些怀疑。到目前为止,我尝试只使用CloseableHttpClient和CloseableHttpResponse,对地址进行rest post调用,然后返回响应(但到目前为止,我无法正确读取响应,因为方法EntityUtils.getString()一直在抛出异常…在Java+;中向远程地址发送http请求并返回响应的方法的数量;弹簧靴,java,spring-boot,httprequest,httpresponse,remote-server,Java,Spring Boot,Httprequest,Httpresponse,Remote Server,我对将请求发送到远程地址,然后使用Java语言在Springboot中返回该响应的可能方式有些怀疑。到目前为止,我尝试只使用CloseableHttpClient和CloseableHttpResponse,对地址进行rest post调用,然后返回响应(但到目前为止,我无法正确读取响应,因为方法EntityUtils.getString()一直在抛出异常… 是否有人对如何实现这一点有其他想法,是否有其他可能的方法可以在这些技术中发送HTTP请求(带有标题和正文)并读取响应?(或者至少在一些其他
是否有人对如何实现这一点有其他想法,是否有其他可能的方法可以在这些技术中发送HTTP请求(带有标题和正文)并读取响应?(或者至少在一些其他技术中,如果在这些技术中不可能的话)。
我将非常感谢任何帮助或建议。据我所知,在
Spring Boot
中进行API
请求有两种常见方法
WebClient
下面是WebClient
发布请求示例:
@Autowired
private WebClient.Builder webClientBuilder;
Turnover turnover = new Turnover();
Gson resp = webClientBuilder.build()
.post()
.uri("url")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON )
.body(Mono.just(turnover),Turnover.class)
.retrieve()
.bodyToMono(Gson.class).block();
overflower.java
@Getter
@Setter
public class Turnover {
private String start_date;
private String end_date;
private String account;
public Turnover(){
setStart_date("01.01.2020");
setEnd_date("01.06.2020");
setAccount("20293435454");
}
}
webclientbuilderbean
。就我而言,我有代理
。所以我使用了代理url和端口
@Bean
public WebClient.Builder getWebClientBuilder(){
HttpClient httpClient = HttpClient.create()
.tcpConfiguration(tcpClient ->
tcpClient.proxy(proxy -> proxy.type(ProxyProvider.Proxy.HTTP).host("url").port(portnumber)));
ReactorClientHttpConnector connector = new ReactorClientHttpConnector(httpClient);
return WebClient.builder().clientConnector(connector);
}
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
org.springframework.boot
弹簧启动器webflux
别忘了在主java类中创建WebClient的
Bean
。上面我只举了一个例子。您需要根据您的需求更改参数。据我所知,在Spring Boot
中进行API
request有两种常见方法
WebClient
下面是WebClient
发布请求示例:
@Autowired
private WebClient.Builder webClientBuilder;
Turnover turnover = new Turnover();
Gson resp = webClientBuilder.build()
.post()
.uri("url")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON )
.body(Mono.just(turnover),Turnover.class)
.retrieve()
.bodyToMono(Gson.class).block();
overflower.java
@Getter
@Setter
public class Turnover {
private String start_date;
private String end_date;
private String account;
public Turnover(){
setStart_date("01.01.2020");
setEnd_date("01.06.2020");
setAccount("20293435454");
}
}
webclientbuilderbean
。就我而言,我有代理
。所以我使用了代理url和端口
@Bean
public WebClient.Builder getWebClientBuilder(){
HttpClient httpClient = HttpClient.create()
.tcpConfiguration(tcpClient ->
tcpClient.proxy(proxy -> proxy.type(ProxyProvider.Proxy.HTTP).host("url").port(portnumber)));
ReactorClientHttpConnector connector = new ReactorClientHttpConnector(httpClient);
return WebClient.builder().clientConnector(connector);
}
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
org.springframework.boot
弹簧启动器webflux
别忘了在主java类中创建WebClient的
Bean
。上面我只举了一个例子。您需要根据您的要求更改参数。您将发送到API
的内容作为POST
请求?我正在尝试发送二进制文件、图像或视频。您将发送到API
的内容作为POST
请求?我正在尝试发送二进制文件、图像或视频。非常感谢您的回答!)您能否解释一下什么是WebClient Builder,以及如何将其包含在代码中?正如我所发现的,第一种方法由于某些原因对我不起作用。非常感谢您的时间。更新了我的答案。@slomil,但是restemplate
更易于使用。因为互联网上有很多资源和例子。可能您在使用restemplate
时出错了。两者之间的主要区别是,WebClient
是异步的。谢谢!只有一个问题,我应该从某处导入WebClient,还是创建WebClient类?看来我无法导入它。。我也看了这里,有依赖项要添加,但仍然无法导入..(如果应该这样做的话)..抱歉,导入正在工作..您还需要在pom.xml
中声明webflux
库WebFlux
主要用于reactive
Spring。更新了我的答案。非常感谢您的回答!:)您能否解释一下什么是WebClient Builder,以及如何将其包含在代码中?正如我所发现的,第一种方法由于某些原因对我不起作用。非常感谢您的时间。更新了我的答案。@slomil,但是restemplate
更易于使用。因为互联网上有很多资源和例子。可能您在使用restemplate
时出错了。两者之间的主要区别是,WebClient
是异步的。谢谢!只有一个问题,我应该从某处导入WebClient,还是创建WebClient类?看来我无法导入它。。我也看了这里,有依赖项要添加,但仍然无法导入..(如果应该这样做的话)..抱歉,导入正在工作..您还需要在pom.xml
中声明webflux
库WebFlux
主要用于reactive
Spring。更新了我的答案。