From 8ad06829931964305b451e85d95f66f6422a15f3 Mon Sep 17 00:00:00 2001
From: osser-sam <46674082+osser-sam@users.noreply.github.com>
Date: Tue, 16 Feb 2021 22:55:41 +0530
Subject: [PATCH] JAVA-4338: Upgrade spring-5-reactive-client module (#10486)
* JAVA-4338: Upgrade spring-5-reactive-client module
* JAVA-4338: removed duplicate entry
---
spring-5-reactive-client/README.md | 1 -
spring-5-reactive-client/pom.xml | 1 -
.../src/main/resources/application.properties | 4 +-
.../WebClientLoggingIntegrationTest.java | 55 +++++++++----------
.../reactive/logging/netty/CustomLogger.java | 42 --------------
5 files changed, 30 insertions(+), 73 deletions(-)
delete mode 100644 spring-5-reactive-client/src/test/java/com/baeldung/reactive/logging/netty/CustomLogger.java
diff --git a/spring-5-reactive-client/README.md b/spring-5-reactive-client/README.md
index b247a1669b..154a3cab0b 100644
--- a/spring-5-reactive-client/README.md
+++ b/spring-5-reactive-client/README.md
@@ -8,7 +8,6 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
### Relevant Articles
- [Logging Spring WebClient Calls](https://www.baeldung.com/spring-log-webclient-calls)
- [Simultaneous Spring WebClient Calls](https://www.baeldung.com/spring-webclient-simultaneous-calls)
-- [Logging Spring WebClient Calls](https://www.baeldung.com/spring-log-webclient-calls)
- [Mocking a WebClient in Spring](https://www.baeldung.com/spring-mocking-webclient)
- [Spring WebClient Filters](https://www.baeldung.com/spring-webclient-filters)
- [Get List of JSON Objects with WebClient](https://www.baeldung.com/spring-webclient-json-list)
diff --git a/spring-5-reactive-client/pom.xml b/spring-5-reactive-client/pom.xml
index 5b773cc63f..7ae7ba6edd 100644
--- a/spring-5-reactive-client/pom.xml
+++ b/spring-5-reactive-client/pom.xml
@@ -176,7 +176,6 @@
4.1
1.0.3
4.0.1
- 2.3.3.RELEASE
diff --git a/spring-5-reactive-client/src/main/resources/application.properties b/spring-5-reactive-client/src/main/resources/application.properties
index 2d93456aeb..05033054b1 100644
--- a/spring-5-reactive-client/src/main/resources/application.properties
+++ b/spring-5-reactive-client/src/main/resources/application.properties
@@ -1,3 +1,5 @@
logging.level.root=INFO
-server.port=8081
\ No newline at end of file
+server.port=8081
+
+logging.level.reactor.netty.http.client.HttpClient=DEBUG
\ No newline at end of file
diff --git a/spring-5-reactive-client/src/test/java/com/baeldung/reactive/logging/WebClientLoggingIntegrationTest.java b/spring-5-reactive-client/src/test/java/com/baeldung/reactive/logging/WebClientLoggingIntegrationTest.java
index 95c63f267f..bb4e682481 100644
--- a/spring-5-reactive-client/src/test/java/com/baeldung/reactive/logging/WebClientLoggingIntegrationTest.java
+++ b/spring-5-reactive-client/src/test/java/com/baeldung/reactive/logging/WebClientLoggingIntegrationTest.java
@@ -1,13 +1,13 @@
package com.baeldung.reactive.logging;
-import ch.qos.logback.classic.spi.LoggingEvent;
-import ch.qos.logback.core.Appender;
-import com.baeldung.reactive.logging.filters.LogFilters;
-import com.baeldung.reactive.logging.netty.CustomLogger;
-import com.fasterxml.jackson.databind.ObjectMapper;
+import static com.baeldung.reactive.logging.jetty.RequestLogEnhancer.enhance;
+import static org.mockito.ArgumentMatchers.argThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
import java.net.URI;
-import lombok.AllArgsConstructor;
-import lombok.Data;
+
import org.eclipse.jetty.client.api.Request;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.junit.jupiter.api.BeforeEach;
@@ -17,14 +17,17 @@ import org.springframework.http.client.reactive.JettyClientHttpConnector;
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.WebClient;
-import reactor.netty.channel.BootstrapHandlers;
-import reactor.netty.http.client.HttpClient;
-import static com.baeldung.reactive.logging.jetty.RequestLogEnhancer.enhance;
-import static org.mockito.ArgumentMatchers.argThat;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
+import com.baeldung.reactive.logging.filters.LogFilters;
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+import ch.qos.logback.classic.spi.LoggingEvent;
+import ch.qos.logback.core.Appender;
+import io.netty.handler.logging.LogLevel;
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import reactor.netty.http.client.HttpClient;
+import reactor.netty.transport.logging.AdvancedByteBufFormat;
public class WebClientLoggingIntegrationTest {
@@ -114,21 +117,17 @@ public class WebClientLoggingIntegrationTest {
@Test
public void givenNettyHttpClientWithCustomLogger_whenEndpointIsConsumed_thenRequestAndResponseBodyLogged() {
+ reactor.netty.http.client.HttpClient httpClient = HttpClient.create()
+ .wiretap("reactor.netty.http.client.HttpClient", LogLevel.DEBUG, AdvancedByteBufFormat.TEXTUAL);
- reactor.netty.http.client.HttpClient httpClient = HttpClient
- .create()
- .tcpConfiguration(
- tc -> tc.bootstrap(
- b -> BootstrapHandlers.updateLogSupport(b, new CustomLogger(HttpClient.class))));
- WebClient
- .builder()
- .clientConnector(new ReactorClientHttpConnector(httpClient))
- .build()
- .post()
- .uri(sampleUrl)
- .body(BodyInserters.fromObject(post))
- .exchange()
- .block();
+ WebClient.builder()
+ .clientConnector(new ReactorClientHttpConnector(httpClient))
+ .build()
+ .post()
+ .uri(sampleUrl)
+ .body(BodyInserters.fromObject(post))
+ .exchange()
+ .block();
verify(nettyAppender).doAppend(argThat(argument -> (((LoggingEvent) argument).getFormattedMessage()).contains(sampleResponseBody)));
}
diff --git a/spring-5-reactive-client/src/test/java/com/baeldung/reactive/logging/netty/CustomLogger.java b/spring-5-reactive-client/src/test/java/com/baeldung/reactive/logging/netty/CustomLogger.java
deleted file mode 100644
index 9f2a4d127f..0000000000
--- a/spring-5-reactive-client/src/test/java/com/baeldung/reactive/logging/netty/CustomLogger.java
+++ /dev/null
@@ -1,42 +0,0 @@
-package com.baeldung.reactive.logging.netty;
-
-import io.netty.buffer.ByteBuf;
-import io.netty.channel.ChannelHandlerContext;
-import io.netty.handler.logging.LoggingHandler;
-import java.nio.charset.Charset;
-
-import static io.netty.util.internal.PlatformDependent.allocateUninitializedArray;
-import static java.lang.Math.max;
-import static java.nio.charset.Charset.defaultCharset;
-
-public class CustomLogger extends LoggingHandler {
- public CustomLogger(Class> clazz) {
- super(clazz);
- }
-
- @Override
- protected String format(ChannelHandlerContext ctx, String event, Object arg) {
- if (arg instanceof ByteBuf) {
- ByteBuf msg = (ByteBuf) arg;
- return decode(msg, msg.readerIndex(), msg.readableBytes(), defaultCharset());
- }
- return super.format(ctx, event, arg);
- }
-
- private String decode(ByteBuf src, int readerIndex, int len, Charset charset) {
- if (len != 0) {
- byte[] array;
- int offset;
- if (src.hasArray()) {
- array = src.array();
- offset = src.arrayOffset() + readerIndex;
- } else {
- array = allocateUninitializedArray(max(len, 1024));
- offset = 0;
- src.getBytes(readerIndex, array, 0, len);
- }
- return new String(array, offset, len, charset);
- }
- return "";
- }
-}