Thoughts about Observable (stream), Single (one element), toList() and Spring
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Sun, 11 Dec 2016 22:06:55 +0000 (23:06 +0100)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Sun, 11 Dec 2016 22:11:42 +0000 (23:11 +0100)
SpringJava/RxJava/web-services-spring-rxjava-server/src/main/java/de/spring/webservices/rest/controller/RxJavaCarController.java
SpringJava/RxJava/web-services-spring-rxjava-server/src/main/java/de/spring/webservices/rest/controller/adapters/RxJavaAdapter.java
SpringJava/RxJava/web-services-spring-rxjava-server/src/test/java/de/spring/webservices/rest/controller/RxJavaCarControllerIntegrationTest.java

index 450a7c4..7a1029c 100644 (file)
@@ -45,8 +45,14 @@ public class RxJavaCarController {
        @RequestMapping(produces = { MediaType.APPLICATION_JSON_UTF8_VALUE }, method = RequestMethod.GET)
     @ResponseStatus(HttpStatus.OK)
     public DeferredResult<Page<Car>> 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<List<Car>> 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<List<Car>> 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<ResponseEntity<Car>> 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<List<Car>> 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<Car> createResponseCar(Car car) {
index a2e9920..e7e7537 100644 (file)
@@ -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);