From e820fbb593d16535785d9a052fcc418177c08291 Mon Sep 17 00:00:00 2001 From: DOHA Date: Mon, 2 Oct 2017 13:08:30 +0200 Subject: [PATCH] update spring 5 --- spring-5/pom.xml | 24 ++---------- ...Spring5URLPatternUsingRouterFunctions.java | 2 +- .../FunctionalSpringBootApplication.java | 25 ++++++------ .../functional/FunctionalWebApplication.java | 25 ++++++------ .../com/baeldung/functional/RootServlet.java | 33 ++++++++-------- .../com/baeldung/jupiter/SpringExtension.java | 38 +++++++++++-------- 6 files changed, 74 insertions(+), 73 deletions(-) diff --git a/spring-5/pom.xml b/spring-5/pom.xml index b77d89b532..4dfede4dab 100644 --- a/spring-5/pom.xml +++ b/spring-5/pom.xml @@ -14,7 +14,7 @@ org.springframework.boot spring-boot-starter-parent - 2.0.0.M1 + 2.0.0.M3 @@ -143,14 +143,6 @@ - - spring-snapshots - Spring Snapshots - https://repo.spring.io/snapshot - - true - - spring-milestones Spring Milestones @@ -161,14 +153,6 @@ - - spring-snapshots - Spring Snapshots - https://repo.spring.io/snapshot - - true - - spring-milestones Spring Milestones @@ -183,10 +167,10 @@ UTF-8 UTF-8 1.8 - 1.0.0-M4 - 5.0.0-M4 + 1.0.0 + 5.0.0 2.20 - 5.0.0.RC2 + 5.0.0.RELEASE 1.0.1.RELEASE 1.1.3 1.0 diff --git a/spring-5/src/main/java/com/baeldung/functional/ExploreSpring5URLPatternUsingRouterFunctions.java b/spring-5/src/main/java/com/baeldung/functional/ExploreSpring5URLPatternUsingRouterFunctions.java index b7436c5ba7..2a6d04538c 100644 --- a/spring-5/src/main/java/com/baeldung/functional/ExploreSpring5URLPatternUsingRouterFunctions.java +++ b/spring-5/src/main/java/com/baeldung/functional/ExploreSpring5URLPatternUsingRouterFunctions.java @@ -33,7 +33,7 @@ public class ExploreSpring5URLPatternUsingRouterFunctions { WebServer start() throws Exception { WebHandler webHandler = (WebHandler) toHttpHandler(routingFunction()); HttpHandler httpHandler = WebHttpHandlerBuilder.webHandler(webHandler) - .prependFilter(new IndexRewriteFilter()) + .filter(new IndexRewriteFilter()) .build(); Tomcat tomcat = new Tomcat(); diff --git a/spring-5/src/main/java/com/baeldung/functional/FunctionalSpringBootApplication.java b/spring-5/src/main/java/com/baeldung/functional/FunctionalSpringBootApplication.java index f181dcb8e8..402b607b19 100644 --- a/spring-5/src/main/java/com/baeldung/functional/FunctionalSpringBootApplication.java +++ b/spring-5/src/main/java/com/baeldung/functional/FunctionalSpringBootApplication.java @@ -1,5 +1,17 @@ package com.baeldung.functional; +import static org.springframework.web.reactive.function.BodyInserters.fromObject; +import static org.springframework.web.reactive.function.server.RequestPredicates.GET; +import static org.springframework.web.reactive.function.server.RequestPredicates.POST; +import static org.springframework.web.reactive.function.server.RequestPredicates.path; +import static org.springframework.web.reactive.function.server.RouterFunctions.route; +import static org.springframework.web.reactive.function.server.RouterFunctions.toHttpHandler; +import static org.springframework.web.reactive.function.server.ServerResponse.ok; + +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; + import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.ServletRegistrationBean; @@ -17,18 +29,9 @@ 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.core.publisher.Flux; -import java.util.Arrays; -import java.util.List; -import java.util.concurrent.CopyOnWriteArrayList; - -import static org.springframework.web.reactive.function.BodyInserters.fromObject; -import static org.springframework.web.reactive.function.server.RequestPredicates.*; -import static org.springframework.web.reactive.function.server.RouterFunctions.route; -import static org.springframework.web.reactive.function.server.RouterFunctions.toHttpHandler; -import static org.springframework.web.reactive.function.server.ServerResponse.ok; - @SpringBootApplication @ComponentScan(basePackages = { "com.baeldung.functional" }) public class FunctionalSpringBootApplication { @@ -57,7 +60,7 @@ public class FunctionalSpringBootApplication { @Bean public ServletRegistrationBean servletRegistrationBean() throws Exception { HttpHandler httpHandler = WebHttpHandlerBuilder.webHandler((WebHandler) toHttpHandler(routingFunction())) - .prependFilter(new IndexRewriteFilter()) + .filter(new IndexRewriteFilter()) .build(); ServletRegistrationBean registrationBean = new ServletRegistrationBean<>(new RootServlet(httpHandler), "/"); registrationBean.setLoadOnStartup(1); diff --git a/spring-5/src/main/java/com/baeldung/functional/FunctionalWebApplication.java b/spring-5/src/main/java/com/baeldung/functional/FunctionalWebApplication.java index 2a4642c484..5a7d70d3db 100644 --- a/spring-5/src/main/java/com/baeldung/functional/FunctionalWebApplication.java +++ b/spring-5/src/main/java/com/baeldung/functional/FunctionalWebApplication.java @@ -1,5 +1,17 @@ package com.baeldung.functional; +import static org.springframework.web.reactive.function.BodyInserters.fromObject; +import static org.springframework.web.reactive.function.server.RequestPredicates.GET; +import static org.springframework.web.reactive.function.server.RequestPredicates.POST; +import static org.springframework.web.reactive.function.server.RequestPredicates.path; +import static org.springframework.web.reactive.function.server.RouterFunctions.route; +import static org.springframework.web.reactive.function.server.RouterFunctions.toHttpHandler; +import static org.springframework.web.reactive.function.server.ServerResponse.ok; + +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; @@ -12,18 +24,9 @@ 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.core.publisher.Flux; -import java.util.Arrays; -import java.util.List; -import java.util.concurrent.CopyOnWriteArrayList; - -import static org.springframework.web.reactive.function.BodyInserters.fromObject; -import static org.springframework.web.reactive.function.server.RequestPredicates.*; -import static org.springframework.web.reactive.function.server.RouterFunctions.route; -import static org.springframework.web.reactive.function.server.RouterFunctions.toHttpHandler; -import static org.springframework.web.reactive.function.server.ServerResponse.ok; - public class FunctionalWebApplication { private static final Actor BRAD_PITT = new Actor("Brad", "Pitt"); @@ -50,7 +53,7 @@ public class FunctionalWebApplication { WebServer start() throws Exception { WebHandler webHandler = (WebHandler) toHttpHandler(routingFunction()); HttpHandler httpHandler = WebHttpHandlerBuilder.webHandler(webHandler) - .prependFilter(new IndexRewriteFilter()) + .filter(new IndexRewriteFilter()) .build(); Tomcat tomcat = new Tomcat(); diff --git a/spring-5/src/main/java/com/baeldung/functional/RootServlet.java b/spring-5/src/main/java/com/baeldung/functional/RootServlet.java index c0dd54cb4a..8fe24821de 100644 --- a/spring-5/src/main/java/com/baeldung/functional/RootServlet.java +++ b/spring-5/src/main/java/com/baeldung/functional/RootServlet.java @@ -1,5 +1,20 @@ package com.baeldung.functional; +import static org.springframework.web.reactive.function.BodyExtractors.toDataBuffers; +import static org.springframework.web.reactive.function.BodyExtractors.toFormData; +import static org.springframework.web.reactive.function.BodyInserters.fromObject; +import static org.springframework.web.reactive.function.server.RequestPredicates.GET; +import static org.springframework.web.reactive.function.server.RequestPredicates.POST; +import static org.springframework.web.reactive.function.server.RequestPredicates.path; +import static org.springframework.web.reactive.function.server.RouterFunctions.route; +import static org.springframework.web.reactive.function.server.RouterFunctions.toHttpHandler; +import static org.springframework.web.reactive.function.server.ServerResponse.ok; + +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; +import java.util.concurrent.atomic.AtomicLong; + import org.springframework.core.io.ClassPathResource; import org.springframework.http.server.reactive.HttpHandler; import org.springframework.http.server.reactive.ServletHttpHandlerAdapter; @@ -7,29 +22,17 @@ import org.springframework.util.MultiValueMap; 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.core.publisher.Flux; import reactor.core.publisher.Mono; -import java.util.Arrays; -import java.util.List; -import java.util.concurrent.CopyOnWriteArrayList; -import java.util.concurrent.atomic.AtomicLong; - -import static org.springframework.web.reactive.function.BodyExtractors.toDataBuffers; -import static org.springframework.web.reactive.function.BodyExtractors.toFormData; -import static org.springframework.web.reactive.function.BodyInserters.fromObject; -import static org.springframework.web.reactive.function.server.RequestPredicates.*; -import static org.springframework.web.reactive.function.server.RouterFunctions.route; -import static org.springframework.web.reactive.function.server.RouterFunctions.toHttpHandler; -import static org.springframework.web.reactive.function.server.ServerResponse.ok; -import org.springframework.web.server.WebHandler; - public class RootServlet extends ServletHttpHandlerAdapter { public RootServlet() { this(WebHttpHandlerBuilder.webHandler((WebHandler) toHttpHandler(routingFunction())) - .prependFilter(new IndexRewriteFilter()) + .filter(new IndexRewriteFilter()) .build()); } diff --git a/spring-5/src/main/java/com/baeldung/jupiter/SpringExtension.java b/spring-5/src/main/java/com/baeldung/jupiter/SpringExtension.java index 0eb7c861f1..7218d984ef 100644 --- a/spring-5/src/main/java/com/baeldung/jupiter/SpringExtension.java +++ b/spring-5/src/main/java/com/baeldung/jupiter/SpringExtension.java @@ -1,28 +1,36 @@ package com.baeldung.jupiter; -import org.junit.jupiter.api.extension.*; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.ApplicationContext; -import org.springframework.core.annotation.AnnotatedElementUtils; -import org.springframework.test.context.TestContextManager; -import org.springframework.util.Assert; - import java.lang.reflect.Constructor; import java.lang.reflect.Executable; import java.lang.reflect.Method; import java.lang.reflect.Parameter; +import org.junit.jupiter.api.extension.AfterAllCallback; +import org.junit.jupiter.api.extension.AfterEachCallback; +import org.junit.jupiter.api.extension.BeforeAllCallback; +import org.junit.jupiter.api.extension.BeforeEachCallback; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.jupiter.api.extension.ParameterContext; +import org.junit.jupiter.api.extension.ParameterResolutionException; +import org.junit.jupiter.api.extension.ParameterResolver; +import org.junit.jupiter.api.extension.TestInstancePostProcessor; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; +import org.springframework.core.annotation.AnnotatedElementUtils; +import org.springframework.test.context.TestContextManager; +import org.springframework.util.Assert; + public class SpringExtension implements BeforeAllCallback, AfterAllCallback, TestInstancePostProcessor, BeforeEachCallback, AfterEachCallback, ParameterResolver { private static final ExtensionContext.Namespace namespace = ExtensionContext.Namespace.create(SpringExtension.class); @Override - public void beforeAll(ContainerExtensionContext context) throws Exception { + public void beforeAll(ExtensionContext context) throws Exception { getTestContextManager(context).beforeTestClass(); } @Override - public void afterAll(ContainerExtensionContext context) throws Exception { + public void afterAll(ExtensionContext context) throws Exception { try { getTestContextManager(context).afterTestClass(); } finally { @@ -38,7 +46,7 @@ public class SpringExtension implements BeforeAllCallback, AfterAllCallback, Tes } @Override - public void beforeEach(TestExtensionContext context) throws Exception { + public void beforeEach(ExtensionContext context) throws Exception { Object testInstance = context.getTestInstance(); Method testMethod = context.getTestMethod() .get(); @@ -46,24 +54,24 @@ public class SpringExtension implements BeforeAllCallback, AfterAllCallback, Tes } @Override - public void afterEach(TestExtensionContext context) throws Exception { + public void afterEach(ExtensionContext context) throws Exception { Object testInstance = context.getTestInstance(); Method testMethod = context.getTestMethod() .get(); - Throwable testException = context.getTestException() + Throwable testException = context.getExecutionException() .orElse(null); getTestContextManager(context).afterTestMethod(testInstance, testMethod, testException); } @Override - public boolean supports(ParameterContext parameterContext, ExtensionContext extensionContext) { + public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException { Parameter parameter = parameterContext.getParameter(); Executable executable = parameter.getDeclaringExecutable(); - return (executable instanceof Constructor && AnnotatedElementUtils.hasAnnotation(executable, Autowired.class)) || ParameterAutowireUtils.isAutowirable(parameter); + return ((executable instanceof Constructor) && AnnotatedElementUtils.hasAnnotation(executable, Autowired.class)) || ParameterAutowireUtils.isAutowirable(parameter); } @Override - public Object resolve(ParameterContext parameterContext, ExtensionContext extensionContext) { + public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException { Parameter parameter = parameterContext.getParameter(); Class testClass = extensionContext.getTestClass() .get();