Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!)
like image 699
runnerpaul Avatar asked Aug 20 '18 08:08

runnerpaul


3 Answers

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.

like image 135
Michael Dally Avatar answered Nov 04 '22 23:11

Michael Dally


Could be lombok dependancy issue. Try writing setter and getter methods instead of lombok.

like image 35
Venkky Avatar answered Nov 04 '22 23:11

Venkky


As stated on this page and adapted to your use case:

And the answer is Yes. Flux<Chapter> represents a stream of Chapters. But, by default, it will produce a JSON array because If a stream of individual JSON objects is sent to the browser then It will not be a valid JSON document as a whole. A browser client has no way to consume a stream other than using Server-Sent-Events or WebSocket.

However, Non-browser clients can request a stream of JSON by setting the Accept header to application/stream+json, and the response will be a stream of JSON similar to Server-Sent-Events but without extra formatting :

So in your case you request the result in your Browser. If you would add the appropriate accept header to application/stream+json you will get the wanted output.

like image 37
mrkernelpanic Avatar answered Nov 04 '22 23:11

mrkernelpanic