From b47e749a4afd9d84860a9636738a93ddf8cf4a0f Mon Sep 17 00:00:00 2001 From: Jacek Polom Date: Sun, 20 Jun 2021 21:01:03 +0200 Subject: [PATCH] BAEL-4447 Enable logging for Apache HttpClient --- httpclient-2/pom.xml | 6 ++++ .../ApacheHttpClient5UnitTest.java | 29 +++++++++++++++++++ .../ApacheHttpClientUnitTest.java | 6 ++-- httpclient-2/src/test/resources/logback.xml | 15 ++++++++++ 4 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 httpclient-2/src/test/java/com/baeldung/httpclient/readresponsebodystring/ApacheHttpClient5UnitTest.java create mode 100644 httpclient-2/src/test/resources/logback.xml diff --git a/httpclient-2/pom.xml b/httpclient-2/pom.xml index 881b0407c3..a6b2ede900 100644 --- a/httpclient-2/pom.xml +++ b/httpclient-2/pom.xml @@ -26,6 +26,11 @@ + + org.apache.httpcomponents.client5 + httpclient5 + ${httpclient5.version} + org.springframework.boot @@ -63,6 +68,7 @@ 4.5.8 + 5.1 11 11 2.1.7.RELEASE diff --git a/httpclient-2/src/test/java/com/baeldung/httpclient/readresponsebodystring/ApacheHttpClient5UnitTest.java b/httpclient-2/src/test/java/com/baeldung/httpclient/readresponsebodystring/ApacheHttpClient5UnitTest.java new file mode 100644 index 0000000000..35b21789ef --- /dev/null +++ b/httpclient-2/src/test/java/com/baeldung/httpclient/readresponsebodystring/ApacheHttpClient5UnitTest.java @@ -0,0 +1,29 @@ +package com.baeldung.httpclient.readresponsebodystring; + +import org.apache.hc.client5.http.classic.methods.HttpGet; +import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; +import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse; +import org.apache.hc.client5.http.impl.classic.HttpClients; +import org.apache.hc.core5.http.HttpEntity; +import org.apache.hc.core5.http.ParseException; +import org.apache.hc.core5.http.io.entity.EntityUtils; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; + +public class ApacheHttpClient5UnitTest { + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + public static final String DUMMY_URL = "https://postman-echo.com/get"; + + @Test + public void whenUseApacheHttpClient_thenCorrect() throws IOException, ParseException { + HttpGet request = new HttpGet(DUMMY_URL); + + try (CloseableHttpClient client = HttpClients.createDefault(); CloseableHttpResponse response = client.execute(request)) { + HttpEntity entity = response.getEntity(); + logger.debug("Response -> {}", EntityUtils.toString(entity)); + } + } +} \ No newline at end of file diff --git a/httpclient-2/src/test/java/com/baeldung/httpclient/readresponsebodystring/ApacheHttpClientUnitTest.java b/httpclient-2/src/test/java/com/baeldung/httpclient/readresponsebodystring/ApacheHttpClientUnitTest.java index 5a638b2bd5..7625b415f2 100644 --- a/httpclient-2/src/test/java/com/baeldung/httpclient/readresponsebodystring/ApacheHttpClientUnitTest.java +++ b/httpclient-2/src/test/java/com/baeldung/httpclient/readresponsebodystring/ApacheHttpClientUnitTest.java @@ -7,10 +7,13 @@ 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 org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.io.IOException; public class ApacheHttpClientUnitTest { + private final Logger logger = LoggerFactory.getLogger(this.getClass()); public static final String DUMMY_URL = "https://postman-echo.com/get"; @Test @@ -19,8 +22,7 @@ public class ApacheHttpClientUnitTest { try (CloseableHttpClient client = HttpClients.createDefault(); CloseableHttpResponse response = client.execute(request)) { HttpEntity entity = response.getEntity(); - String result = EntityUtils.toString(entity); - System.out.println("Response -> " + result); + logger.debug("Response -> {}", EntityUtils.toString(entity)); } } } diff --git a/httpclient-2/src/test/resources/logback.xml b/httpclient-2/src/test/resources/logback.xml new file mode 100644 index 0000000000..366a94e86e --- /dev/null +++ b/httpclient-2/src/test/resources/logback.xml @@ -0,0 +1,15 @@ + + + + %date [%level] %logger - %msg %n + + + + + + + + + + + \ No newline at end of file