Merge pull request #10461 from osser-sam/JAVA-4312

JAVA-4312: Update deprecations in spring-5-reactive module
This commit is contained in:
Dhawal Kapil 2021-02-04 22:35:33 +05:30 committed by GitHub
commit 030b5cca02
13 changed files with 33 additions and 33 deletions

View File

@ -11,7 +11,7 @@ import java.util.concurrent.atomic.AtomicLong;
import static org.springframework.web.reactive.function.BodyExtractors.toDataBuffers; 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.BodyExtractors.toFormData;
import static org.springframework.web.reactive.function.BodyInserters.fromObject; import static org.springframework.web.reactive.function.BodyInserters.fromValue;
import static org.springframework.web.reactive.function.server.ServerResponse.ok; import static org.springframework.web.reactive.function.server.ServerResponse.ok;
public class FormHandler { public class FormHandler {
@ -29,7 +29,7 @@ public class FormHandler {
Mono<ServerResponse> handleUpload(ServerRequest request) { Mono<ServerResponse> handleUpload(ServerRequest request) {
return request.body(toDataBuffers()) return request.body(toDataBuffers())
.collectList() .collectList()
.flatMap(dataBuffers -> ok().body(fromObject(extractData(dataBuffers).toString()))); .flatMap(dataBuffers -> ok().body(fromValue(extractData(dataBuffers).toString())));
} }
private AtomicLong extractData(List<DataBuffer> dataBuffers) { private AtomicLong extractData(List<DataBuffer> dataBuffers) {

View File

@ -1,6 +1,6 @@
package com.baeldung.functional; package com.baeldung.functional;
import static org.springframework.web.reactive.function.BodyInserters.fromObject; import static org.springframework.web.reactive.function.BodyInserters.fromValue;
import static org.springframework.web.reactive.function.server.RequestPredicates.GET; 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.POST;
import static org.springframework.web.reactive.function.server.RequestPredicates.path; import static org.springframework.web.reactive.function.server.RequestPredicates.path;
@ -44,7 +44,7 @@ public class FunctionalSpringBootApplication {
.doOnNext(actors::add) .doOnNext(actors::add)
.then(ok().build())); .then(ok().build()));
return route(GET("/test"), serverRequest -> ok().body(fromObject("helloworld"))) return route(GET("/test"), serverRequest -> ok().body(fromValue("helloworld")))
.andRoute(POST("/login"), formHandler::handleLogin) .andRoute(POST("/login"), formHandler::handleLogin)
.andRoute(POST("/upload"), formHandler::handleUpload) .andRoute(POST("/upload"), formHandler::handleUpload)
.and(RouterFunctions.resources("/files/**", new ClassPathResource("files/"))) .and(RouterFunctions.resources("/files/**", new ClassPathResource("files/")))

View File

@ -1,6 +1,6 @@
package com.baeldung.functional; package com.baeldung.functional;
import static org.springframework.web.reactive.function.BodyInserters.fromObject; import static org.springframework.web.reactive.function.BodyInserters.fromValue;
import static org.springframework.web.reactive.function.server.RequestPredicates.GET; 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.POST;
import static org.springframework.web.reactive.function.server.RequestPredicates.accept; import static org.springframework.web.reactive.function.server.RequestPredicates.accept;
@ -42,7 +42,7 @@ public class FunctionalWebApplication {
.doOnNext(actors::add) .doOnNext(actors::add)
.then(ok().build())); .then(ok().build()));
return route(GET("/test"), serverRequest -> ok().body(fromObject("helloworld"))).andRoute(POST("/login"), formHandler::handleLogin) return route(GET("/test"), serverRequest -> ok().body(fromValue("helloworld"))).andRoute(POST("/login"), formHandler::handleLogin)
.andRoute(POST("/upload"), formHandler::handleUpload) .andRoute(POST("/upload"), formHandler::handleUpload)
.and(RouterFunctions.resources("/files/**", new ClassPathResource("files/"))) .and(RouterFunctions.resources("/files/**", new ClassPathResource("files/")))
.andNest(accept(MediaType.APPLICATION_JSON), restfulRouter) .andNest(accept(MediaType.APPLICATION_JSON), restfulRouter)

View File

@ -2,7 +2,7 @@ package com.baeldung.functional;
import static org.springframework.web.reactive.function.BodyExtractors.toDataBuffers; 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.BodyExtractors.toFormData;
import static org.springframework.web.reactive.function.BodyInserters.fromObject; import static org.springframework.web.reactive.function.BodyInserters.fromValue;
import static org.springframework.web.reactive.function.server.RequestPredicates.GET; 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.POST;
import static org.springframework.web.reactive.function.server.RequestPredicates.path; import static org.springframework.web.reactive.function.server.RequestPredicates.path;
@ -46,7 +46,7 @@ public class RootServlet extends ServletHttpHandlerAdapter {
private static RouterFunction<?> routingFunction() { private static RouterFunction<?> routingFunction() {
return route(GET("/test"), serverRequest -> ok().body(fromObject("helloworld"))).andRoute(POST("/login"), serverRequest -> serverRequest.body(toFormData()) return route(GET("/test"), serverRequest -> ok().body(fromValue("helloworld"))).andRoute(POST("/login"), serverRequest -> serverRequest.body(toFormData())
.map(MultiValueMap::toSingleValueMap) .map(MultiValueMap::toSingleValueMap)
.map(formData -> { .map(formData -> {
System.out.println("form data: " + formData.toString()); System.out.println("form data: " + formData.toString());
@ -65,7 +65,7 @@ public class RootServlet extends ServletHttpHandlerAdapter {
dataBuffers.forEach(d -> atomicLong.addAndGet(d.asByteBuffer() dataBuffers.forEach(d -> atomicLong.addAndGet(d.asByteBuffer()
.array().length)); .array().length));
System.out.println("data length:" + atomicLong.get()); System.out.println("data length:" + atomicLong.get());
return ok().body(fromObject(atomicLong.toString())) return ok().body(fromValue(atomicLong.toString()))
.block(); .block();
})) }))
.and(RouterFunctions.resources("/files/**", new ClassPathResource("files/"))) .and(RouterFunctions.resources("/files/**", new ClassPathResource("files/")))

View File

@ -2,7 +2,8 @@
package com.baeldung.reactive.errorhandling; package com.baeldung.reactive.errorhandling;
import java.util.Map; import java.util.Map;
import org.springframework.boot.autoconfigure.web.ResourceProperties;
import org.springframework.boot.autoconfigure.web.WebProperties;
import org.springframework.boot.autoconfigure.web.reactive.error.AbstractErrorWebExceptionHandler; import org.springframework.boot.autoconfigure.web.reactive.error.AbstractErrorWebExceptionHandler;
import org.springframework.boot.web.error.ErrorAttributeOptions; import org.springframework.boot.web.error.ErrorAttributeOptions;
import org.springframework.boot.web.reactive.error.ErrorAttributes; import org.springframework.boot.web.reactive.error.ErrorAttributes;
@ -18,6 +19,7 @@ import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions; import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerRequest; import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse; import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
@Component @Component
@ -26,7 +28,7 @@ public class GlobalErrorWebExceptionHandler extends AbstractErrorWebExceptionHan
public GlobalErrorWebExceptionHandler(GlobalErrorAttributes g, ApplicationContext applicationContext, public GlobalErrorWebExceptionHandler(GlobalErrorAttributes g, ApplicationContext applicationContext,
ServerCodecConfigurer serverCodecConfigurer) { ServerCodecConfigurer serverCodecConfigurer) {
super(g, new ResourceProperties(), applicationContext); super(g, new WebProperties.Resources(), applicationContext);
super.setMessageWriters(serverCodecConfigurer.getWriters()); super.setMessageWriters(serverCodecConfigurer.getWriters());
super.setMessageReaders(serverCodecConfigurer.getReaders()); super.setMessageReaders(serverCodecConfigurer.getReaders());
} }
@ -41,8 +43,8 @@ public class GlobalErrorWebExceptionHandler extends AbstractErrorWebExceptionHan
final Map<String, Object> errorPropertiesMap = getErrorAttributes(request, ErrorAttributeOptions.defaults()); final Map<String, Object> errorPropertiesMap = getErrorAttributes(request, ErrorAttributeOptions.defaults());
return ServerResponse.status(HttpStatus.BAD_REQUEST) return ServerResponse.status(HttpStatus.BAD_REQUEST)
.contentType(MediaType.APPLICATION_JSON_UTF8) .contentType(MediaType.APPLICATION_JSON)
.body(BodyInserters.fromObject(errorPropertiesMap)); .body(BodyInserters.fromValue(errorPropertiesMap));
} }
} }

View File

@ -14,7 +14,7 @@ public class Handler1 {
return sayHello(request).onErrorReturn("Hello, Stranger") return sayHello(request).onErrorReturn("Hello, Stranger")
.flatMap(s -> ServerResponse.ok() .flatMap(s -> ServerResponse.ok()
.contentType(MediaType.TEXT_PLAIN) .contentType(MediaType.TEXT_PLAIN)
.syncBody(s)); .bodyValue(s));
} }
private Mono<String> sayHello(ServerRequest request) { private Mono<String> sayHello(ServerRequest request) {

View File

@ -15,11 +15,11 @@ public Mono<ServerResponse> handleRequest2(ServerRequest request) {
sayHello(request) sayHello(request)
.flatMap(s -> ServerResponse.ok() .flatMap(s -> ServerResponse.ok()
.contentType(MediaType.TEXT_PLAIN) .contentType(MediaType.TEXT_PLAIN)
.syncBody(s)) .bodyValue(s))
.onErrorResume(e -> sayHelloFallback() .onErrorResume(e -> sayHelloFallback()
.flatMap(s -> ServerResponse.ok() .flatMap(s -> ServerResponse.ok()
.contentType(MediaType.TEXT_PLAIN) .contentType(MediaType.TEXT_PLAIN)
.syncBody(s))); .bodyValue(s)));
} }
private Mono<String> sayHello(ServerRequest request) { private Mono<String> sayHello(ServerRequest request) {

View File

@ -15,11 +15,11 @@ public class Handler3 {
sayHello(request) sayHello(request)
.flatMap(s -> ServerResponse.ok() .flatMap(s -> ServerResponse.ok()
.contentType(MediaType.TEXT_PLAIN) .contentType(MediaType.TEXT_PLAIN)
.syncBody(s)) .bodyValue(s))
.onErrorResume(e -> (Mono.just("Hi, I looked around for your name but found: " + .onErrorResume(e -> (Mono.just("Hi, I looked around for your name but found: " +
e.getMessage())).flatMap(s -> ServerResponse.ok() e.getMessage())).flatMap(s -> ServerResponse.ok()
.contentType(MediaType.TEXT_PLAIN) .contentType(MediaType.TEXT_PLAIN)
.syncBody(s))); .bodyValue(s)));
} }
private Mono<String> sayHello(ServerRequest request) { private Mono<String> sayHello(ServerRequest request) {

View File

@ -1,6 +1,6 @@
package com.baeldung.reactive.urlmatch; package com.baeldung.reactive.urlmatch;
import static org.springframework.web.reactive.function.BodyInserters.fromObject; import static org.springframework.web.reactive.function.BodyInserters.fromValue;
import static org.springframework.web.reactive.function.server.RequestPredicates.GET; import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
import static org.springframework.web.reactive.function.server.RouterFunctions.route; 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.RouterFunctions.toHttpHandler;
@ -24,10 +24,10 @@ public class ExploreSpring5URLPatternUsingRouterFunctions {
private RouterFunction<ServerResponse> routingFunction() { private RouterFunction<ServerResponse> routingFunction() {
return route(GET("/p?ths"), serverRequest -> ok().body(fromObject("/p?ths"))).andRoute(GET("/test/{*id}"), serverRequest -> ok().body(fromObject(serverRequest.pathVariable("id")))) return route(GET("/p?ths"), serverRequest -> ok().body(fromValue("/p?ths"))).andRoute(GET("/test/{*id}"), serverRequest -> ok().body(fromValue(serverRequest.pathVariable("id"))))
.andRoute(GET("/*card"), serverRequest -> ok().body(fromObject("/*card path was accessed"))) .andRoute(GET("/*card"), serverRequest -> ok().body(fromValue("/*card path was accessed")))
.andRoute(GET("/{var1}_{var2}"), serverRequest -> ok().body(fromObject(serverRequest.pathVariable("var1") + " , " + serverRequest.pathVariable("var2")))) .andRoute(GET("/{var1}_{var2}"), serverRequest -> ok().body(fromValue(serverRequest.pathVariable("var1") + " , " + serverRequest.pathVariable("var2"))))
.andRoute(GET("/{baeldung:[a-z]+}"), serverRequest -> ok().body(fromObject("/{baeldung:[a-z]+} was accessed and baeldung=" + serverRequest.pathVariable("baeldung")))) .andRoute(GET("/{baeldung:[a-z]+}"), serverRequest -> ok().body(fromValue("/{baeldung:[a-z]+} was accessed and baeldung=" + serverRequest.pathVariable("baeldung"))))
.and(RouterFunctions.resources("/files/{*filepaths}", new ClassPathResource("files/"))); .and(RouterFunctions.resources("/files/{*filepaths}", new ClassPathResource("files/")));
} }

View File

@ -11,7 +11,7 @@ import java.util.concurrent.atomic.AtomicLong;
import static org.springframework.web.reactive.function.BodyExtractors.toDataBuffers; 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.BodyExtractors.toFormData;
import static org.springframework.web.reactive.function.BodyInserters.fromObject; import static org.springframework.web.reactive.function.BodyInserters.fromValue;
import static org.springframework.web.reactive.function.server.ServerResponse.ok; import static org.springframework.web.reactive.function.server.ServerResponse.ok;
public class FormHandler { public class FormHandler {
@ -29,7 +29,7 @@ public class FormHandler {
Mono<ServerResponse> handleUpload(ServerRequest request) { Mono<ServerResponse> handleUpload(ServerRequest request) {
return request.body(toDataBuffers()) return request.body(toDataBuffers())
.collectList() .collectList()
.flatMap(dataBuffers -> ok().body(fromObject(extractData(dataBuffers).toString()))); .flatMap(dataBuffers -> ok().body(fromValue(extractData(dataBuffers).toString())));
} }
private AtomicLong extractData(List<DataBuffer> dataBuffers) { private AtomicLong extractData(List<DataBuffer> dataBuffers) {

View File

@ -1,6 +1,6 @@
package com.baeldung.reactive.urlmatch; package com.baeldung.reactive.urlmatch;
import static org.springframework.web.reactive.function.BodyInserters.fromObject; import static org.springframework.web.reactive.function.BodyInserters.fromValue;
import static org.springframework.web.reactive.function.server.RequestPredicates.GET; 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.POST;
import static org.springframework.web.reactive.function.server.RequestPredicates.path; import static org.springframework.web.reactive.function.server.RequestPredicates.path;
@ -40,7 +40,7 @@ public class FunctionalWebApplication {
.doOnNext(actors::add) .doOnNext(actors::add)
.then(ok().build())); .then(ok().build()));
return route(GET("/test"), serverRequest -> ok().body(fromObject("helloworld"))).andRoute(POST("/login"), formHandler::handleLogin) return route(GET("/test"), serverRequest -> ok().body(fromValue("helloworld"))).andRoute(POST("/login"), formHandler::handleLogin)
.andRoute(POST("/upload"), formHandler::handleUpload) .andRoute(POST("/upload"), formHandler::handleUpload)
.and(RouterFunctions.resources("/files/**", new ClassPathResource("files/"))) .and(RouterFunctions.resources("/files/**", new ClassPathResource("files/")))
.andNest(path("/actor"), restfulRouter) .andNest(path("/actor"), restfulRouter)

View File

@ -1,17 +1,15 @@
package com.baeldung.functional; package com.baeldung.functional;
import static org.springframework.web.reactive.function.BodyInserters.fromObject; import static org.springframework.web.reactive.function.BodyInserters.fromValue;
import static org.springframework.web.reactive.function.BodyInserters.fromResource; import static org.springframework.web.reactive.function.BodyInserters.fromResource;
import org.junit.AfterClass; import org.junit.AfterClass;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.web.server.WebServer; import org.springframework.boot.web.server.WebServer;
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.web.reactive.server.WebTestClient; import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap; import org.springframework.util.MultiValueMap;
@ -115,7 +113,7 @@ public class FunctionalWebApplicationIntegrationTest {
client.post() client.post()
.uri("/actor") .uri("/actor")
.body(fromObject(new Actor("Clint", "Eastwood"))) .body(fromValue(new Actor("Clint", "Eastwood")))
.exchange() .exchange()
.expectStatus() .expectStatus()
.isOk(); .isOk();

View File

@ -133,7 +133,7 @@ public class ErrorHandlingIntegrationTest {
.expectStatus() .expectStatus()
.isBadRequest() .isBadRequest()
.expectHeader() .expectHeader()
.contentType(MediaType.APPLICATION_JSON_UTF8) .contentType(MediaType.APPLICATION_JSON)
.expectBody() .expectBody()
.jsonPath("$.message") .jsonPath("$.message")
.isNotEmpty() .isNotEmpty()
@ -164,7 +164,7 @@ public class ErrorHandlingIntegrationTest {
.expectStatus() .expectStatus()
.isBadRequest() .isBadRequest()
.expectHeader() .expectHeader()
.contentType(MediaType.APPLICATION_JSON_UTF8) .contentType(MediaType.APPLICATION_JSON)
.expectBody() .expectBody()
.jsonPath("$.message") .jsonPath("$.message")
.isNotEmpty() .isNotEmpty()