Search code examples
javaspringeclipsespring-bootspring-tool-suite

Spring application returning empty JSON


I've started reading "Learning Spring Boot 2.0 - Second Edition: Simplify the development of lightning fast applications based on microservices and reactive programming" and am having trouble with one of the first sample programs.

When I do a GET on http://localhost:8080/chapters it returns:

[
    {},
    {},
    {}
]

instead of:

[
    {"id": 1,
     "name": "Quick Start with Java"},
    {"id":,
     "name": "Reactive Web with Spring Boot"},
    {"id": 3,
     "name": ... and more!"}
]

This is my code(minus imports):

@Data
@Document
public class Chapter {

    @Id
    private String id;
    private String name;

    public Chapter(String name) {
        this.name = name;
    }
}


public interface ChapterRepository extends ReactiveCrudRepository<Chapter, String>{
}

@Configuration
public class LoadDatabase {

@Bean
CommandLineRunner init(ChapterRepository repository) {
    return args -> {
        Flux.just(
                new Chapter("Quick Start with Java"),
                new Chapter("Reactive Web with Spring Boot"),
                new Chapter("... and more!"))
        .flatMap(repository::save)
        .subscribe(System.out::println);
        };
    }
}

@RestController
public class ChapterController {

    private final ChapterRepository repository;

    public ChapterController(ChapterRepository repository) {
        this.repository = repository;
    }

    @GetMapping("/chapters")
    public Flux<Chapter> listing() {
        return repository.findAll();
    }

}

Is there something wrong with my code?

Additional Information

As stated in a comment below, I've had bit of a development. Previously I had only tried running this in my IDE. I use gradle to build the project, ./gradlew clean build and ran it from my terminal with java -jar build/libs/learning-spring-boot-0.0.1-SNAPSHOT.jar and I get the proper response. Works for Insomnia, Postman, browser and curl. This allows me to cintinue with my work but I'd love to get this resolved for my IDE. I'm using Spring Tool suite for Eclipse.

One thing I noticed is that when I start the server in STS the console output finishes with this:

2018-09-09 09:54:50.646  INFO 12073 --- [ntLoopGroup-2-3] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:4, serverValue:31}] to localhost:27017
2018-09-09 09:54:50.647  INFO 12073 --- [ntLoopGroup-2-2] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:3, serverValue:30}] to localhost:27017
2018-09-09 09:54:50.649  INFO 12073 --- [ntLoopGroup-2-4] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:5, serverValue:32}] to localhost:27017
    com.greglturnquist.learningspringboot.learningspringboot.Chapter@c56c194
    com.greglturnquist.learningspringboot.learningspringboot.Chapter@795759ac
    com.greglturnquist.learningspringboot.learningspringboot.Chapter@76e125f2

but when run in the terminal I can see the book names:

2018-09-09 09:52:42.768  INFO 12058 --- [ntLoopGroup-2-2] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:3, serverValue:25}] to localhost:27017
2018-09-09 09:52:42.789  INFO 12058 --- [ntLoopGroup-2-3] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:4, serverValue:26}] to localhost:27017
2018-09-09 09:52:42.791  INFO 12058 --- [ntLoopGroup-2-4] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:5, serverValue:27}] to localhost:27017
Chapter(id=5b94df5ac0b4b02f1af43f43, name=Quick Start with Java)
Chapter(id=5b94df5ac0b4b02f1af43f44, name=Reactive Web with Spring Boot)
Chapter(id=5b94df5ac0b4b02f1af43f45, name=...and more!)

Solution

  • I've had the very same issue, and for me I had to ensure my IDE had Lombok annotation processing enabled (I'm using IntelliJ Ultimate). When enabling this and restarting my app, I started seeing data as expected and not empty JSON arrays.