0

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!

Ravenix
  • 908
  • 2
  • 14
  • 34
  • 1
    It looks to me like your `Chapter` object has no getter for its id and name properties. How are you running the code? Is it somewhere that knows about Lombok and will have run its annotation processor? Lombok may reduce the amount of code you have to write but its reliance on an annotation processor adds a considerable amount of complexity. – Andy Wilkinson Nov 04 '18 at 18:11

1 Answers1

2

You are getting empty objects because the data is not saved and something went wrong.

You are using @Data lombok annotation which is like having implicit @Getter, @Setter, @ToString, @EqualsAndHashCode and @RequiredArgsConstructor annotations on the class (except that no constructor will be generated if any explicitly written constructor exists). but sometimes it doesn't work if not configured in IDE correctly so try once with manual getters and setters for the properties.

If the manual getters/setters work then try below to troubleshoot for lombok.

Ensure that your IDE is aware of lombok.

IntelliJ : Lombok added but getters and setters not recognized in Intellij IDEA

Eclipse : Lombok is not generating getter and setter

If the problem still exists then follow this similar thread's one of the comments here

Alien
  • 11,017
  • 3
  • 29
  • 45