Spring 应用程序返回空 JSON
Spring application returning empty JSON
我已经开始阅读 "Learning Spring Boot 2.0 - Second Edition: Simplify the development of lightning fast applications based on microservices and reactive programming",但在使用第一个示例程序时遇到了问题。
当我在 http://localhost:8080/chapters
上执行 GET 操作时 returns:
[
{},
{},
{}
]
而不是:
[
{"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 中尝试过 运行ning 这个。我使用 gradle 构建项目,./gradlew clean build 和 运行 从我的终端使用 java -jar build/libs/learning-spring-boot-0.0.1-SNAPSHOT.jar,我得到了正确的回应。适用于 Insomnia、Postman、浏览器和 curl。这让我可以继续我的工作,但我很乐意为我的 IDE 解决这个问题。我正在使用 Spring Eclipse 工具套件。
我注意到的一件事是,当我在 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!)
如 this 页面所述并适应您的用例:
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 :
因此,在您的情况下,您在浏览器中请求结果。如果您将适当的 accept header
添加到 application/stream+json
,您将获得所需的输出。
可能是 lombok 依赖问题。尝试编写 setter 和 getter 方法而不是 lombok。
我遇到了同样的问题,对我来说,我必须确保我的 IDE 启用了 Lombok 注释处理(我使用的是 IntelliJ Ultimate)。启用此功能并重新启动我的应用程序时,我开始看到预期的数据,而不是空 JSON 数组。
在我的例子中,前一个 answers/tips 的 none 解决了它。
解决方案是在我的实体中实施:
@Override public int hashCode()
@Override public boolean equals(Object)
我已经开始阅读 "Learning Spring Boot 2.0 - Second Edition: Simplify the development of lightning fast applications based on microservices and reactive programming",但在使用第一个示例程序时遇到了问题。
当我在 http://localhost:8080/chapters
上执行 GET 操作时 returns:
[
{},
{},
{}
]
而不是:
[
{"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 中尝试过 运行ning 这个。我使用 gradle 构建项目,./gradlew clean build 和 运行 从我的终端使用 java -jar build/libs/learning-spring-boot-0.0.1-SNAPSHOT.jar,我得到了正确的回应。适用于 Insomnia、Postman、浏览器和 curl。这让我可以继续我的工作,但我很乐意为我的 IDE 解决这个问题。我正在使用 Spring Eclipse 工具套件。
我注意到的一件事是,当我在 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!)
如 this 页面所述并适应您的用例:
And the answer is Yes.
Flux<Chapter>
represents a stream ofChapters
. 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 :
因此,在您的情况下,您在浏览器中请求结果。如果您将适当的 accept header
添加到 application/stream+json
,您将获得所需的输出。
可能是 lombok 依赖问题。尝试编写 setter 和 getter 方法而不是 lombok。
我遇到了同样的问题,对我来说,我必须确保我的 IDE 启用了 Lombok 注释处理(我使用的是 IntelliJ Ultimate)。启用此功能并重新启动我的应用程序时,我开始看到预期的数据,而不是空 JSON 数组。
在我的例子中,前一个 answers/tips 的 none 解决了它。
解决方案是在我的实体中实施:
@Override public int hashCode()
@Override public boolean equals(Object)