Skip to content

Commit 0a7a6e8

Browse files
committed
BAEL-4447 Enable logging for Apache HttpClient
1 parent 3f96496 commit 0a7a6e8

4 files changed

Lines changed: 54 additions & 2 deletions

File tree

httpclient-2/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@
2626
</exclusion>
2727
</exclusions>
2828
</dependency>
29+
<dependency>
30+
<groupId>org.apache.httpcomponents.client5</groupId>
31+
<artifactId>httpclient5</artifactId>
32+
<version>${httpclient5.version}</version>
33+
</dependency>
2934
<!-- rest template -->
3035
<dependency>
3136
<groupId>org.springframework.boot</groupId>
@@ -63,6 +68,7 @@
6368

6469
<properties>
6570
<httpclient.version>4.5.8</httpclient.version>
71+
<httpclient5.version>5.1</httpclient5.version>
6672
<maven.compiler.source.version>11</maven.compiler.source.version>
6773
<maven.compiler.target.version>11</maven.compiler.target.version>
6874
<spring-boot.version>2.1.7.RELEASE</spring-boot.version>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.baeldung.httpclient.readresponsebodystring;
2+
3+
import org.apache.hc.client5.http.classic.methods.HttpGet;
4+
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
5+
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
6+
import org.apache.hc.client5.http.impl.classic.HttpClients;
7+
import org.apache.hc.core5.http.HttpEntity;
8+
import org.apache.hc.core5.http.ParseException;
9+
import org.apache.hc.core5.http.io.entity.EntityUtils;
10+
import org.junit.Test;
11+
import org.slf4j.Logger;
12+
import org.slf4j.LoggerFactory;
13+
14+
import java.io.IOException;
15+
16+
public class ApacheHttpClient5UnitTest {
17+
private final Logger logger = LoggerFactory.getLogger(this.getClass());
18+
public static final String DUMMY_URL = "https://postman-echo.com/get";
19+
20+
@Test
21+
public void whenUseApacheHttpClient_thenCorrect() throws IOException, ParseException {
22+
HttpGet request = new HttpGet(DUMMY_URL);
23+
24+
try (CloseableHttpClient client = HttpClients.createDefault(); CloseableHttpResponse response = client.execute(request)) {
25+
HttpEntity entity = response.getEntity();
26+
logger.debug("Response -> {}", EntityUtils.toString(entity));
27+
}
28+
}
29+
}

httpclient-2/src/test/java/com/baeldung/httpclient/readresponsebodystring/ApacheHttpClientUnitTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@
77
import org.apache.http.impl.client.HttpClients;
88
import org.apache.http.util.EntityUtils;
99
import org.junit.Test;
10+
import org.slf4j.Logger;
11+
import org.slf4j.LoggerFactory;
1012

1113
import java.io.IOException;
1214

1315
public class ApacheHttpClientUnitTest {
16+
private final Logger logger = LoggerFactory.getLogger(this.getClass());
1417
public static final String DUMMY_URL = "https://postman-echo.com/get";
1518

1619
@Test
@@ -19,8 +22,7 @@ public void whenUseApacheHttpClient_thenCorrect() throws IOException {
1922

2023
try (CloseableHttpClient client = HttpClients.createDefault(); CloseableHttpResponse response = client.execute(request)) {
2124
HttpEntity entity = response.getEntity();
22-
String result = EntityUtils.toString(entity);
23-
System.out.println("Response -> " + result);
25+
logger.debug("Response -> {}", EntityUtils.toString(entity));
2426
}
2527
}
2628
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<configuration debug="false">
2+
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
3+
<encoder>
4+
<pattern>%date [%level] %logger - %msg %n</pattern>
5+
</encoder>
6+
</appender>
7+
8+
<logger name="com.baeldung.httpclient.readresponsebodystring" level="debug"/>
9+
<logger name="org.apache.http" level="debug"/>
10+
<logger name="org.apache.hc.client5.http" level="debug"/>
11+
12+
<root level="WARN">
13+
<appender-ref ref="stdout"/>
14+
</root>
15+
</configuration>

0 commit comments

Comments
 (0)