Firstly, take in account is my first question here in CodeReview. Any purpose misuderstanding will be highly appreciated if orientaded.
Scenario: I want to answer randomly a pojo (json with two fields) to Angular using a WebFlux restservice. I can succesfully answer an integer but I got stuck while trying retruning a model. On top of that I am using Server Sent Events but I don't think it is relevant to this question.
Successfully done with an Integer:
@GetMapping(value = "/randomNumbers", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<ServerSentEvent<Integer>> randomNumbers() {
return Flux.interval(Duration.ofSeconds(1)).map(seq -> Tuples.of(seq, ThreadLocalRandom.current().nextInt()))
.map(data -> ServerSentEvent.<Integer>builder().event("random").id(Long.toString(data.getT1()))
.data(data.getT2()).build());
}
Completely stuck trying to follow same approach to answer a status model:
@GetMapping(value = "/randomStatus", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<ServerSentEvent<Status>> randomStatus() {
ArrayList<String> statusList = new ArrayList<String>();
statusList.add("Iniciado");
statusList.add("Recusado");
statusList.add("Sucesso");
// for (int i = 0; i < 100; i++) {
// int x = ThreadLocalRandom.current().nextInt(0, statusList.size());
// System.out.println(statusList.get(x));
// }
//the next line it is not working but I pasted to show where I am stuck. Let's say I can't create a new Status object return Flux.interval(Duration.ofSeconds(1)) .map(seq -> Tuples.of(seq, ThreadLocalRandom.current().nextInt())) .map(data -> ServerSentEvent.builder().event("random").data(new Status(Long.toString(data.getT1()), ThreadLocalRandom.current().nextInt(0, statusList.size() ) .data(data.getT2()).build()))); }
}
In case it is relevant, here is Status model:
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
@Document
public class Status {
@Id
private String id;
private String status;
... getters/setters/contructor hidden for simplicity
Imports in case it is relevants are:
import java.time.Duration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.codec.ServerSentEvent;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import com.reactive.fluxdemo.domain.Status;
import com.reactive.fluxdemo.domain.Transfer;
import com.reactive.fluxdemo.repository.StatusRepository;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.util.function.Tuples;
import java.util.ArrayList;
import java.util.concurrent.ThreadLocalRandom;
Code Review aims to help improve working code. If you are trying to figure out why your program crashes or produces a wrong result, ask on Stack Overflow instead. Code Review is also not the place to ask for implementing new features.\$\endgroup\$