瀏覽代碼

Flux responses

dagarcos 2 年之前
父節點
當前提交
5484dbe1ba

+ 8 - 0
src/main/java/es/uv/garcosda/controllers/NewsController.java

@@ -1,9 +1,11 @@
 package es.uv.garcosda.controllers;
 
 import java.text.SimpleDateFormat;
+import java.time.Duration;
 import java.util.Date;
 
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.MediaType;
 import org.springframework.web.bind.annotation.CrossOrigin;
 import org.springframework.web.bind.annotation.DeleteMapping;
 import org.springframework.web.bind.annotation.GetMapping;
@@ -16,6 +18,7 @@ import org.springframework.web.bind.annotation.RestController;
 
 import es.uv.garcosda.domain.Document;
 import es.uv.garcosda.services.NewsService;
+import reactor.core.publisher.Flux;
 import reactor.core.publisher.Mono;
 
 @RestController
@@ -31,6 +34,11 @@ public class NewsController {
         return this.ns.findById(id);
     }
 	
+	@GetMapping(produces = MediaType.TEXT_EVENT_STREAM_VALUE)
+	public Flux<Document> getAll() {
+		return this.ns.findAll().delayElements(Duration.ofMillis(500));
+	}
+	
 	@PostMapping()
 	public Mono<Document> insert(@RequestBody Document n){
 		n.setDateAdd(new SimpleDateFormat("dd-MM-yyyy").format(new Date()));

+ 4 - 0
src/main/java/es/uv/garcosda/services/NewsService.java

@@ -22,6 +22,10 @@ public class NewsService {
 		return this.nr.findById(id);
 	}
 	
+	public Flux<Document> findAll() {
+		return this.nr.findAll();
+	}
+	
 	public Mono<Document> update(Document n){
 		return this.nr.save(n);
 	}