簡體   English   中英

Spring應用程序返回空的JSON

[英]Spring application returning empty JSON

我已經開始閱讀“學習 Spring Boot 2.0 - 第二版:簡化基於微服務和反應式編程的閃電般快速應用程序的開發”,並且在使用第一個示例程序時遇到了問題。

當我在http://localhost:8080/chapters上執行 GET 時,它返回:

[
    {},
    {},
    {}
]

代替:

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

這是我的代碼(減去進口):

@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();
    }

}

我的代碼有問題嗎?

附加信息

正如下面的評論中所述,我有了一些發展。 以前我只嘗試在我的 IDE 中運行它。 我使用 gradle 構建項目, ./gradlew clean build 並使用 java -jar build/libs/learning-spring-boot-0.0.1-SNAPSHOT.jar 從我的終端運行它,我得到了正確的響應。 適用於 Insomnia、Postman、瀏覽器和 curl。 這讓我可以繼續我的工作,但我很想為我的 IDE 解決這個問題。 我正在為 Eclipse 使用 Spring 工具套件。

我注意到的一件事是,當我在 STS 中啟動服務器時,控制台輸出會結束:

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

但是在終端中運行時,我可以看到書名:

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!)

我遇到了同樣的問題,對我來說,我必須確保我的 IDE 啟用了 Lombok 注釋處理(我使用的是 IntelliJ Ultimate)。 啟用此功能並重新啟動我的應用程序時,我開始按預期看到數據,而不是空的 JSON 數組。

如前所述在頁面,並適用於你的使用情況:

答案是肯定的。 Flux<Chapter>代表一個Chapters流。 但是,默認情況下,它會生成一個 JSON 數組,因為如果將單個 JSON 對象的流發送到瀏覽器,那么它作為一個整體將不是有效的 JSON 文檔。 除了使用 Server-Sent-Events 或 WebSocket 之外,瀏覽器客戶端無法使用流。

但是,非瀏覽器客戶端可以通過將 Accept 標頭設置為application/stream+json來請求 JSON application/stream+json ,並且響應將是類似於 Server-Sent-Events 但沒有額外格式的 JSON 流:

因此,在您的情況下,您在瀏覽器中請求結果。 如果您將適當的accept header添加到application/stream+json您將獲得所需的輸出。

可能是 lombok 依賴問題。 嘗試編寫 setter 和 getter 方法而不是 lombok。

更改以下代碼

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

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

因為它會在內部將您的列表轉換為JSON。

就我而言,以前的答案/提示都沒有解決它。

解決方案是在我的實體中實施:

  • @Override public int hashCode()
  • @Override public boolean equals(Object)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM