Merge pull request #9916 from oussama-benmahmoud/BAEL-4443-reading-http-response-body-string
BAEL-4443 Reading HTTP ResponseBody as a String
This commit is contained in:
commit
214710beb8
|
@ -4,6 +4,7 @@
|
|||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>httpclient-2</artifactId>
|
||||
<version>0.1-SNAPSHOT</version>
|
||||
<name>httpclient-2</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
|
@ -13,6 +14,7 @@
|
|||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<!-- http client -->
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpclient</artifactId>
|
||||
|
@ -24,6 +26,19 @@
|
|||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<!-- rest template -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
<version>${spring-boot.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<version>${spring-boot.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
@ -34,10 +49,24 @@
|
|||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
<configuration>
|
||||
<source>${maven.compiler.source.version}</source>
|
||||
<target>${maven.compiler.target.version}</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<httpclient.version>4.5.8</httpclient.version>
|
||||
<maven.compiler.source.version>11</maven.compiler.source.version>
|
||||
<maven.compiler.target.version>11</maven.compiler.target.version>
|
||||
<spring-boot.version>2.1.7.RELEASE</spring-boot.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,26 @@
|
|||
package com.baeldung.httpclient.readresponsebodystring;
|
||||
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class ApacheHttpClientUnitTest {
|
||||
public static final String DUMMY_URL = "https://postman-echo.com/get";
|
||||
|
||||
@Test
|
||||
public void whenUseApacheHttpClient_thenCorrect() throws IOException {
|
||||
HttpGet request = new HttpGet(DUMMY_URL);
|
||||
|
||||
try (CloseableHttpClient client = HttpClients.createDefault(); CloseableHttpResponse response = client.execute(request)) {
|
||||
HttpEntity entity = response.getEntity();
|
||||
String result = EntityUtils.toString(entity);
|
||||
System.out.println("Response -> " + result);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
package com.baeldung.httpclient.readresponsebodystring;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
|
||||
public class HttpClientUnitTest {
|
||||
public static final String DUMMY_URL = "https://postman-echo.com/get";
|
||||
|
||||
@Test
|
||||
public void whenUseHttpClient_thenCorrect() throws IOException, InterruptedException {
|
||||
HttpClient client = HttpClient.newHttpClient();
|
||||
HttpRequest request = HttpRequest.newBuilder().uri(URI.create(DUMMY_URL)).build();
|
||||
|
||||
// synchronous response
|
||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
System.out.println(response.body());
|
||||
|
||||
// asynchronous response
|
||||
client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
|
||||
.thenApply(HttpResponse::body)
|
||||
.thenAccept(System.out::println)
|
||||
.join();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
package com.baeldung.httpclient.readresponsebodystring;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
|
||||
public class HttpUrlConnectionUnitTest {
|
||||
|
||||
public static final String DUMMY_URL = "https://postman-echo.com/get";
|
||||
|
||||
@Test
|
||||
public void whenUseHttpUrlConnection_thenCorrect() throws IOException {
|
||||
HttpURLConnection connection = (HttpURLConnection) new URL(DUMMY_URL).openConnection();
|
||||
|
||||
InputStream inputStream = connection.getInputStream();
|
||||
|
||||
BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
|
||||
StringBuilder response = new StringBuilder();
|
||||
String currentLine;
|
||||
|
||||
while ((currentLine = in.readLine()) != null)
|
||||
response.append(currentLine);
|
||||
|
||||
in.close();
|
||||
Assert.assertNotNull(response.toString());
|
||||
System.out.println("Response -> " + response.toString());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
package com.baeldung.httpclient.readresponsebodystring;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
public class SpringRestTemplateUnitTest {
|
||||
|
||||
public static final String DUMMY_URL = "https://postman-echo.com/get";
|
||||
|
||||
@Test
|
||||
public void whenUseRestTemplate_thenCorrect() {
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
String response = restTemplate.getForObject(DUMMY_URL, String.class);
|
||||
System.out.println(response);
|
||||
}
|
||||
|
||||
}
|
4
pom.xml
4
pom.xml
|
@ -424,7 +424,7 @@
|
|||
<module>hazelcast</module>
|
||||
<module>helidon</module>
|
||||
<module>httpclient</module>
|
||||
<module>httpclient-2</module>
|
||||
<!--<module>httpclient-2</module>--><!-- to be fixed in http://team.baeldung.com/browse/JAVA-2824 -->
|
||||
<module>httpclient-simple</module>
|
||||
<module>hystrix</module>
|
||||
|
||||
|
@ -936,7 +936,7 @@
|
|||
<module>hazelcast</module>
|
||||
<module>helidon</module>
|
||||
<module>httpclient</module>
|
||||
<module>httpclient-2</module>
|
||||
<!--<module>httpclient-2</module>--><!-- to be fixed in http://team.baeldung.com/browse/JAVA-2824 -->
|
||||
<module>httpclient-simple</module>
|
||||
<module>hystrix</module>
|
||||
|
||||
|
|
Loading…
Reference in New Issue