I am starting with Flux today for it is pretty powerful. Now I have set up a whole simple Spring boot 2 project to work with this but the returned objects are empty.
I started a very simple Spring Boot project with some dependencies:
- Reactive Web (embedded Netty + Spring WebFlux)
- Reactive MongoDB (Spring Data MongoDB)
- Thymeleaf template engine
- Lombok (to simplify writing POJOs)
And added some code:
controller:
@RestController
public class ChapterController {
@Autowired
private ChapterRepository repository;
@GetMapping("/chapters")
public Flux<Chapter> listing() {
return repository.findAll();
}
}
Repository:
public interface ChapterRepository extends ReactiveCrudRepository<Chapter, String> {}
Configuration: (to load some data in the embeded Mongodb) @Configuration public class LoadDatabase {
@Bean
CommandLineRunner init(ChapterRepository repository){
return args -> {
Flux.just(
new Chapter("The life of Batman"),
new Chapter("Batmans most glorious' code"),
new Chapter("The hero we needed but didn't deserve, Batman."))
.flatMap(repository::save)
.subscribe(System.out::println);
};
}
}
Data class:
@Data
@Document
public class Chapter {
@Id
private String id;
private String name;
public Chapter(String name) {
this.name = name;
}
}
Ok, now when I start the application and access the endpoint: http://localhost:8080/chapters it returns this:
[
{},
{},
{}
]
It is displaying the same amount of objects that I created in the LoadDatabase
class. When I change the amount of objects created, it shows that amount on the endpoint.
I don't know what I did wrong, I tried debugging the flux object returned. But I can't make anything out of it.
I hope someone can spot my mistake!