发送HTTP请求到远程地址并在Java+Springboot中返回响应的方法数量

huangapple go评论87阅读模式
英文:

Number of ways to send http request to remote address and return response in Java+Springboot

问题

关于使用Java语言在Springboot中向远程地址发送请求并返回响应的可能方法,我有一些疑问。到目前为止,我尝试过仅使用CloseableHttpClient和CloseableHttpResponse来执行REST POST请求到地址,然后返回响应(但迄今为止,我无法正确读取响应,因为EntityUtils.getString()方法一直抛出异常... https://stackoverflow.com/questions/63220523/extracting-json-from-response-as-responseentityproxycontent-type-application )<br>
是否有其他人也许有其他方法可以实现这一点,是否有其他可能的方式可以发送HTTP请求(包括标头和正文)并读取响应,在这些技术中(如果在这些技术中不可能,至少在其他技术中是否可能...)<br>
我将非常感激任何形式的帮助或建议。

英文:

I have some doubts about possible ways of sending a request to a remote address, and then return that response in Springboot using Java language. So far I tried to do that only using CloseableHttpClient and CloseableHttpResponse, making rest post call to the address, and then return a response (yet I've been so far unable to read the response properly, since method EntityUtils.getString() has been throwing exceptions.. https://stackoverflow.com/questions/63220523/extracting-json-from-response-as-responseentityproxycontent-type-application )<br>
Does anyone have maybe some other idea how can that be achieved, are there other possible ways how one can send an HTTP request(with headers and body) and read the response, in these technologies?(or at least in some other technologies if it's not possible in these..).<br>
I would greatly appreciate any kind of help or suggestion.

答案1

得分: 2

据我所知,在Spring Boot中有两种常见的方式来发起API请求。

  1. RestTemplate
  2. WebClient

大多数人今天通常使用RestTemplate。但是它将在未来几年内被弃用。因此,我建议您使用WebClient

以下是WebClientPOST请求示例:

 @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();

Turnover.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");
    }
}

webClientBuilder Bean. 在我的情况下,我使用了PROXY。因此,我使用了代理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>

不要忘记在您的主要Java类中创建WebClient的Bean。上面只是一个示例。您需要根据您的要求更改参数。

英文:

As far as I know there are two common ways to make API reqests in Spring Boot.

  1. RestTemplate
  2. WebClient

Most of people todat use commonly RestTemplate. But it is going to be deprecated in coming years. So that I recommend you to use WebClient.

Below WebClient POST REQUEST Example:

 @Autowired
 private WebClient.Builder webClientBuilder;


 Turnover turnover = new Turnover();
                
 Gson resp = webClientBuilder.build()
 .post()
 .uri(&quot;url&quot;)
 .contentType(MediaType.APPLICATION_JSON)
 .accept(MediaType.APPLICATION_JSON )
 .body(Mono.just(turnover),Turnover.class)
 .retrieve()
 .bodyToMono(Gson.class).block();

Turnover.java

@Getter
@Setter
public class Turnover {

    private String start_date;
    private String end_date;
    private String account;

    public Turnover(){
        setStart_date(&quot;01.01.2020&quot;);
        setEnd_date(&quot;01.06.2020&quot;);
        setAccount(&quot;20293435454&quot;);
    }
}

webClientBuilder Bean. In my case I had PROXY. So that I used proxy url and port.

@Bean
	public WebClient.Builder getWebClientBuilder(){

		HttpClient httpClient = HttpClient.create()
				.tcpConfiguration(tcpClient -&gt;
						tcpClient.proxy(proxy -&gt; proxy.type(ProxyProvider.Proxy.HTTP).host(&quot;url&quot;).port(portnumber)));
		ReactorClientHttpConnector connector = new ReactorClientHttpConnector(httpClient);
		return WebClient.builder().clientConnector(connector);
	}

pom.xml

&lt;dependency&gt;
	&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    &lt;artifactId&gt;spring-boot-starter-webflux&lt;/artifactId&gt;
&lt;/dependency&gt;

And don`t forget to create Bean of WebClient in your Main java class. Above I gave just an example. You need to change arguments based on your requirements.

huangapple
  • 本文由 发表于 2020年8月4日 10:33:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/63239433.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定