From: Gustavo Martin Morcuende Date: Sun, 11 Dec 2016 22:06:55 +0000 (+0100) Subject: Thoughts about Observable (stream), Single (one element), toList() and Spring X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=925de87be3df9780a95ebe43a22827026fe2a1db;p=JavaForFun Thoughts about Observable (stream), Single (one element), toList() and Spring --- diff --git a/SpringJava/RxJava/web-services-spring-rxjava-server/src/main/java/de/spring/webservices/rest/controller/RxJavaCarController.java b/SpringJava/RxJava/web-services-spring-rxjava-server/src/main/java/de/spring/webservices/rest/controller/RxJavaCarController.java index 450a7c4..7a1029c 100644 --- a/SpringJava/RxJava/web-services-spring-rxjava-server/src/main/java/de/spring/webservices/rest/controller/RxJavaCarController.java +++ b/SpringJava/RxJava/web-services-spring-rxjava-server/src/main/java/de/spring/webservices/rest/controller/RxJavaCarController.java @@ -45,8 +45,14 @@ public class RxJavaCarController { @RequestMapping(produces = { MediaType.APPLICATION_JSON_UTF8_VALUE }, method = RequestMethod.GET) @ResponseStatus(HttpStatus.OK) public DeferredResult> cars() { + + // BE CAREFUL: I am returning Page object but when using io.reactivex.Observable (stream) instead of io.reactivex.Single (only one element) + // if you want this code to work you will have to return DeferredResult> and you will have to call + // the toList() method of Observable. + // The toList() method is the only way I know for returning Observable (stream) perhaps in Spring 5.0.0 there will be something better. + // Until then, this is the only way I know for using Observable with Spring. - return deferredAdapter(rxJavaBusinessLogic.findAll(new PageRequest(PAGE, PAGE_SIZE))); + return deferredAdapter(rxJavaBusinessLogic.findAll(new PageRequest(PAGE, PAGE_SIZE)) /** .toList() **/); } @RequestMapping(value = "{id}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, method = RequestMethod.GET) @@ -73,8 +79,14 @@ public class RxJavaCarController { LOGGER.info(wheel); } } + + // BE CAREFUL: I am returning Page object but when using io.reactivex.Observable (stream) instead of io.reactivex.Single (only one element) + // if you want this code to work you will have to return DeferredResult> and you will have to call + // the toList() method of Observable. + // The toList() method is the only way I know for returning Observable (stream) perhaps in Spring 5.0.0 there will be something better. + // Until then, this is the only way I know for using Observable with Spring. - return deferredAdapter(rxJavaBusinessLogic.findById(id)); + return deferredAdapter(rxJavaBusinessLogic.findById(id) /** .toList() **/); } @@ -89,9 +101,15 @@ public class RxJavaCarController { private Observable> createAsync(Car car) { + // BE CAREFUL: I am returning Page object but when using io.reactivex.Observable (stream) instead of io.reactivex.Single (only one element) + // if you want this code to work you will have to return DeferredResult> and you will have to call + // the toList() method of Observable. + // The toList() method is the only way I know for returning Observable (stream) perhaps in Spring 5.0.0 there will be something better. + // Until then, this is the only way I know for using Observable with Spring. + return rxJavaBusinessLogic .createThrowable(car) - .map(this::createResponseCar); + .map(this::createResponseCar) /** .toList() **/; } private ResponseEntity createResponseCar(Car car) { diff --git a/SpringJava/RxJava/web-services-spring-rxjava-server/src/main/java/de/spring/webservices/rest/controller/adapters/RxJavaAdapter.java b/SpringJava/RxJava/web-services-spring-rxjava-server/src/main/java/de/spring/webservices/rest/controller/adapters/RxJavaAdapter.java index a2e9920..e7e7537 100644 --- a/SpringJava/RxJava/web-services-spring-rxjava-server/src/main/java/de/spring/webservices/rest/controller/adapters/RxJavaAdapter.java +++ b/SpringJava/RxJava/web-services-spring-rxjava-server/src/main/java/de/spring/webservices/rest/controller/adapters/RxJavaAdapter.java @@ -8,6 +8,13 @@ import io.reactivex.Observable; import io.reactivex.Single; import io.reactivex.schedulers.Schedulers; +/** + * + * Instead of using this class, you could create you own implementation of + * org.springframework.web.servlet.mvc.method.annotation.DeferredResultAdapter + * for Observable and Single. + * + */ public class RxJavaAdapter { private static final Logger LOGGER = LoggerFactory.getLogger(RxJavaAdapter.class); diff --git a/SpringJava/RxJava/web-services-spring-rxjava-server/src/test/java/de/spring/webservices/rest/controller/RxJavaCarControllerIntegrationTest.java b/SpringJava/RxJava/web-services-spring-rxjava-server/src/test/java/de/spring/webservices/rest/controller/RxJavaCarControllerIntegrationTest.java index e7eaeda..63afdc1 100644 --- a/SpringJava/RxJava/web-services-spring-rxjava-server/src/test/java/de/spring/webservices/rest/controller/RxJavaCarControllerIntegrationTest.java +++ b/SpringJava/RxJava/web-services-spring-rxjava-server/src/test/java/de/spring/webservices/rest/controller/RxJavaCarControllerIntegrationTest.java @@ -18,6 +18,7 @@ import java.util.ArrayList; import java.util.List; import org.junit.Before; +import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.data.domain.Page;