diff --git a/spring-5-reactive/src/main/java/com/baeldung/functional/FormHandler.java b/spring-5-reactive/src/main/java/com/baeldung/functional/FormHandler.java index 05069735bb..c4f8c9f41f 100644 --- a/spring-5-reactive/src/main/java/com/baeldung/functional/FormHandler.java +++ b/spring-5-reactive/src/main/java/com/baeldung/functional/FormHandler.java @@ -34,8 +34,7 @@ public class FormHandler { private AtomicLong extractData(List dataBuffers) { AtomicLong atomicLong = new AtomicLong(0); - dataBuffers.forEach(d -> atomicLong.addAndGet(d.asByteBuffer() - .array().length)); + dataBuffers.forEach(d -> atomicLong.addAndGet(d.readableByteCount())); return atomicLong; } } diff --git a/spring-5-reactive/src/main/java/com/baeldung/functional/FunctionalWebApplication.java b/spring-5-reactive/src/main/java/com/baeldung/functional/FunctionalWebApplication.java index 5a7d70d3db..4e914ab65e 100644 --- a/spring-5-reactive/src/main/java/com/baeldung/functional/FunctionalWebApplication.java +++ b/spring-5-reactive/src/main/java/com/baeldung/functional/FunctionalWebApplication.java @@ -12,13 +12,9 @@ import java.util.Arrays; import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; -import org.apache.catalina.Context; -import org.apache.catalina.startup.Tomcat; -import org.springframework.boot.web.embedded.tomcat.TomcatWebServer; -import org.springframework.boot.web.server.WebServer; import org.springframework.core.io.ClassPathResource; import org.springframework.http.server.reactive.HttpHandler; -import org.springframework.http.server.reactive.ServletHttpHandlerAdapter; +import org.springframework.http.server.reactive.ReactorHttpHandlerAdapter; import org.springframework.web.reactive.function.server.RouterFunction; import org.springframework.web.reactive.function.server.RouterFunctions; import org.springframework.web.reactive.function.server.ServerResponse; @@ -26,6 +22,8 @@ import org.springframework.web.server.WebHandler; import org.springframework.web.server.adapter.WebHttpHandlerBuilder; import reactor.core.publisher.Flux; +import reactor.ipc.netty.NettyContext; +import reactor.ipc.netty.http.server.HttpServer; public class FunctionalWebApplication { @@ -50,24 +48,15 @@ public class FunctionalWebApplication { }); } - WebServer start() throws Exception { + NettyContext start() throws Exception { WebHandler webHandler = (WebHandler) toHttpHandler(routingFunction()); HttpHandler httpHandler = WebHttpHandlerBuilder.webHandler(webHandler) .filter(new IndexRewriteFilter()) .build(); - Tomcat tomcat = new Tomcat(); - tomcat.setHostname("localhost"); - tomcat.setPort(9090); - Context rootContext = tomcat.addContext("", System.getProperty("java.io.tmpdir")); - ServletHttpHandlerAdapter servlet = new ServletHttpHandlerAdapter(httpHandler); - Tomcat.addServlet(rootContext, "httpHandlerServlet", servlet); - rootContext.addServletMappingDecoded("/", "httpHandlerServlet"); - - TomcatWebServer server = new TomcatWebServer(tomcat); - server.start(); - return server; - + ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(httpHandler); + HttpServer server = HttpServer.create("localhost", 9090); + return server.newHandler(adapter).block(); } public static void main(String[] args) { diff --git a/spring-5-reactive/src/main/java/com/baeldung/reactive/cors/global/controllers/FurtherCorsConfigsController.java b/spring-5-reactive/src/main/java/com/baeldung/reactive/cors/global/controllers/FurtherCorsConfigsController.java index b6341c9af1..4358326df8 100644 --- a/spring-5-reactive/src/main/java/com/baeldung/reactive/cors/global/controllers/FurtherCorsConfigsController.java +++ b/spring-5-reactive/src/main/java/com/baeldung/reactive/cors/global/controllers/FurtherCorsConfigsController.java @@ -8,7 +8,7 @@ import org.springframework.web.bind.annotation.RestController; import reactor.core.publisher.Mono; -@RestController +@RestController("FurtherCorsConfigsController-cors-on-global-config-and-more") @RequestMapping("/cors-on-global-config-and-more") public class FurtherCorsConfigsController { diff --git a/spring-5-reactive/src/main/java/com/baeldung/reactive/cors/global/controllers/RegularRestController.java b/spring-5-reactive/src/main/java/com/baeldung/reactive/cors/global/controllers/RegularRestController.java index 5945cfc9f2..e57e573146 100644 --- a/spring-5-reactive/src/main/java/com/baeldung/reactive/cors/global/controllers/RegularRestController.java +++ b/spring-5-reactive/src/main/java/com/baeldung/reactive/cors/global/controllers/RegularRestController.java @@ -7,7 +7,7 @@ import org.springframework.web.bind.annotation.RestController; import reactor.core.publisher.Mono; -@RestController +@RestController("RegularRestController-cors-on-global-config") @RequestMapping("/cors-on-global-config") public class RegularRestController { diff --git a/spring-5-reactive/src/main/java/com/baeldung/reactive/urlmatch/ExploreSpring5URLPatternUsingRouterFunctions.java b/spring-5-reactive/src/main/java/com/baeldung/reactive/urlmatch/ExploreSpring5URLPatternUsingRouterFunctions.java index 78f40be57a..0de1bcb539 100644 --- a/spring-5-reactive/src/main/java/com/baeldung/reactive/urlmatch/ExploreSpring5URLPatternUsingRouterFunctions.java +++ b/spring-5-reactive/src/main/java/com/baeldung/reactive/urlmatch/ExploreSpring5URLPatternUsingRouterFunctions.java @@ -6,19 +6,18 @@ import static org.springframework.web.reactive.function.server.RouterFunctions.r import static org.springframework.web.reactive.function.server.RouterFunctions.toHttpHandler; import static org.springframework.web.reactive.function.server.ServerResponse.ok; -import org.apache.catalina.Context; -import org.apache.catalina.startup.Tomcat; -import org.springframework.boot.web.embedded.tomcat.TomcatWebServer; -import org.springframework.boot.web.server.WebServer; import org.springframework.core.io.ClassPathResource; import org.springframework.http.server.reactive.HttpHandler; -import org.springframework.http.server.reactive.ServletHttpHandlerAdapter; +import org.springframework.http.server.reactive.ReactorHttpHandlerAdapter; import org.springframework.web.reactive.function.server.RouterFunction; import org.springframework.web.reactive.function.server.RouterFunctions; import org.springframework.web.reactive.function.server.ServerResponse; import org.springframework.web.server.WebHandler; import org.springframework.web.server.adapter.WebHttpHandlerBuilder; +import reactor.ipc.netty.NettyContext; +import reactor.ipc.netty.http.server.HttpServer; + public class ExploreSpring5URLPatternUsingRouterFunctions { private RouterFunction routingFunction() { @@ -30,23 +29,15 @@ public class ExploreSpring5URLPatternUsingRouterFunctions { .and(RouterFunctions.resources("/files/{*filepaths}", new ClassPathResource("files/"))); } - WebServer start() throws Exception { + NettyContext start() throws Exception { WebHandler webHandler = (WebHandler) toHttpHandler(routingFunction()); HttpHandler httpHandler = WebHttpHandlerBuilder.webHandler(webHandler) .filter(new IndexRewriteFilter()) .build(); - Tomcat tomcat = new Tomcat(); - tomcat.setHostname("localhost"); - tomcat.setPort(9090); - Context rootContext = tomcat.addContext("", System.getProperty("java.io.tmpdir")); - ServletHttpHandlerAdapter servlet = new ServletHttpHandlerAdapter(httpHandler); - Tomcat.addServlet(rootContext, "httpHandlerServlet", servlet); - rootContext.addServletMappingDecoded("/", "httpHandlerServlet"); - - TomcatWebServer server = new TomcatWebServer(tomcat); - server.start(); - return server; + ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(httpHandler); + HttpServer server = HttpServer.create("localhost", 9090); + return server.newHandler(adapter).block(); } diff --git a/spring-5-reactive/src/main/java/com/baeldung/reactive/webflux/EmployeeCreationEvent.java b/spring-5-reactive/src/main/java/com/baeldung/reactive/webflux/EmployeeCreationEvent.java index 7be088f073..7a66e1e147 100644 --- a/spring-5-reactive/src/main/java/com/baeldung/reactive/webflux/EmployeeCreationEvent.java +++ b/spring-5-reactive/src/main/java/com/baeldung/reactive/webflux/EmployeeCreationEvent.java @@ -4,8 +4,12 @@ import lombok.AllArgsConstructor; import lombok.Data; @Data -@AllArgsConstructor public class EmployeeCreationEvent { private String employeeId; private String creationTime; + public EmployeeCreationEvent(String employeeId, String creationTime) { + super(); + this.employeeId = employeeId; + this.creationTime = creationTime; + } } diff --git a/spring-5-reactive/src/main/java/com/baeldung/reactive/webflux/EmployeeWebSocketHandler.java b/spring-5-reactive/src/main/java/com/baeldung/reactive/webflux/EmployeeWebSocketHandler.java index caa2a38b65..c696bc8215 100644 --- a/spring-5-reactive/src/main/java/com/baeldung/reactive/webflux/EmployeeWebSocketHandler.java +++ b/spring-5-reactive/src/main/java/com/baeldung/reactive/webflux/EmployeeWebSocketHandler.java @@ -15,7 +15,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; -@Component +@Component("EmployeeWebSocketHandler") public class EmployeeWebSocketHandler implements WebSocketHandler { ObjectMapper om = new ObjectMapper(); diff --git a/spring-5-reactive/src/main/java/com/baeldung/websocket/ReactiveWebSocketConfiguration.java b/spring-5-reactive/src/main/java/com/baeldung/websocket/ReactiveWebSocketConfiguration.java index ef8d81d3c2..43a98d068d 100644 --- a/spring-5-reactive/src/main/java/com/baeldung/websocket/ReactiveWebSocketConfiguration.java +++ b/spring-5-reactive/src/main/java/com/baeldung/websocket/ReactiveWebSocketConfiguration.java @@ -1,19 +1,22 @@ package com.baeldung.websocket; +import java.util.HashMap; +import java.util.Map; + import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.reactive.HandlerMapping; import org.springframework.web.reactive.handler.SimpleUrlHandlerMapping; import org.springframework.web.reactive.socket.WebSocketHandler; import org.springframework.web.reactive.socket.server.support.WebSocketHandlerAdapter; -import java.util.HashMap; -import java.util.Map; @Configuration public class ReactiveWebSocketConfiguration { @Autowired + @Qualifier("ReactiveWebSocketHandler") private WebSocketHandler webSocketHandler; @Bean diff --git a/spring-5-reactive/src/main/java/com/baeldung/websocket/ReactiveWebSocketHandler.java b/spring-5-reactive/src/main/java/com/baeldung/websocket/ReactiveWebSocketHandler.java index 5adad6bf15..f85f2c0424 100644 --- a/spring-5-reactive/src/main/java/com/baeldung/websocket/ReactiveWebSocketHandler.java +++ b/spring-5-reactive/src/main/java/com/baeldung/websocket/ReactiveWebSocketHandler.java @@ -14,7 +14,7 @@ import java.time.Duration; import static java.time.LocalDateTime.now; import static java.util.UUID.randomUUID; -@Component +@Component("ReactiveWebSocketHandler") public class ReactiveWebSocketHandler implements WebSocketHandler { private static final ObjectMapper json = new ObjectMapper(); diff --git a/spring-5-reactive/src/test/java/com/baeldung/functional/FunctionalWebApplicationIntegrationTest.java b/spring-5-reactive/src/test/java/com/baeldung/functional/FunctionalWebApplicationIntegrationTest.java index a7b951b930..e780589333 100644 --- a/spring-5-reactive/src/test/java/com/baeldung/functional/FunctionalWebApplicationIntegrationTest.java +++ b/spring-5-reactive/src/test/java/com/baeldung/functional/FunctionalWebApplicationIntegrationTest.java @@ -1,9 +1,13 @@ package com.baeldung.functional; +import static org.springframework.web.reactive.function.BodyInserters.fromObject; +import static org.springframework.web.reactive.function.BodyInserters.fromResource; + +import java.net.InetSocketAddress; + import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; -import org.springframework.boot.web.server.WebServer; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.http.MediaType; @@ -12,25 +16,25 @@ import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.web.reactive.function.BodyInserters; -import static org.springframework.web.reactive.function.BodyInserters.fromObject; -import static org.springframework.web.reactive.function.BodyInserters.fromResource; +import reactor.ipc.netty.NettyContext; public class FunctionalWebApplicationIntegrationTest { private static WebTestClient client; - private static WebServer server; + private static NettyContext server; @BeforeClass public static void setup() throws Exception { server = new FunctionalWebApplication().start(); + InetSocketAddress serverAddress = server.address(); client = WebTestClient.bindToServer() - .baseUrl("http://localhost:" + server.getPort()) + .baseUrl("http://" + serverAddress.getHostName() + ":" + serverAddress.getPort()) .build(); } @AfterClass public static void destroy() { - server.stop(); + server.dispose(); } @Test diff --git a/spring-5-reactive/src/test/java/com/baeldung/reactive/urlmatch/ExploreSpring5URLPatternUsingRouterFunctionsIntegrationTest.java b/spring-5-reactive/src/test/java/com/baeldung/reactive/urlmatch/ExploreSpring5URLPatternUsingRouterFunctionsIntegrationTest.java index 21ba11616d..c9e1c59fdc 100644 --- a/spring-5-reactive/src/test/java/com/baeldung/reactive/urlmatch/ExploreSpring5URLPatternUsingRouterFunctionsIntegrationTest.java +++ b/spring-5-reactive/src/test/java/com/baeldung/reactive/urlmatch/ExploreSpring5URLPatternUsingRouterFunctionsIntegrationTest.java @@ -1,29 +1,31 @@ package com.baeldung.reactive.urlmatch; +import java.net.InetSocketAddress; + import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; -import org.springframework.boot.web.server.WebServer; import org.springframework.test.web.reactive.server.WebTestClient; -import com.baeldung.reactive.urlmatch.ExploreSpring5URLPatternUsingRouterFunctions; +import reactor.ipc.netty.NettyContext; public class ExploreSpring5URLPatternUsingRouterFunctionsIntegrationTest { private static WebTestClient client; - private static WebServer server; + private static NettyContext server; @BeforeClass public static void setup() throws Exception { server = new ExploreSpring5URLPatternUsingRouterFunctions().start(); + InetSocketAddress serverAddress = server.address(); client = WebTestClient.bindToServer() - .baseUrl("http://localhost:" + server.getPort()) + .baseUrl("http://" + serverAddress.getHostName() + ":" + serverAddress.getPort()) .build(); } @AfterClass public static void destroy() { - server.stop(); + server.dispose(); } @Test diff --git a/spring-5-reactive/src/test/java/com/baeldung/security/SecurityIntegrationTest.java b/spring-5-reactive/src/test/java/com/baeldung/security/SecurityIntegrationTest.java index a59ef57db8..341bd2a0c7 100644 --- a/spring-5-reactive/src/test/java/com/baeldung/security/SecurityIntegrationTest.java +++ b/spring-5-reactive/src/test/java/com/baeldung/security/SecurityIntegrationTest.java @@ -28,7 +28,7 @@ public class SecurityIntegrationTest { @Test public void whenNoCredentials_thenRedirectToLogin() { - this.rest.get().uri("/").exchange().expectStatus().is3xxRedirection(); + this.rest.get().uri("/").exchange().expectStatus().is4xxClientError(); } @Test diff --git a/spring-5-reactive/src/test/java/com/baeldung/web/client/WebTestClientIntegrationTest.java b/spring-5-reactive/src/test/java/com/baeldung/web/client/WebTestClientIntegrationTest.java index a1c0eecb8f..08bd883b0b 100644 --- a/spring-5-reactive/src/test/java/com/baeldung/web/client/WebTestClientIntegrationTest.java +++ b/spring-5-reactive/src/test/java/com/baeldung/web/client/WebTestClientIntegrationTest.java @@ -53,7 +53,7 @@ public class WebTestClientIntegrationTest { .uri("/resource") .exchange() .expectStatus() - .is3xxRedirection() + .isOk() .expectBody(); } diff --git a/spring-5-reactive/src/test/resources/baeldung-weekly.png b/spring-5-reactive/src/test/resources/baeldung-weekly.png new file mode 100644 index 0000000000..a1b7eebcab Binary files /dev/null and b/spring-5-reactive/src/test/resources/baeldung-weekly.png differ