run*_*aul 9 java eclipse spring spring-tool-suite spring-boot
我开始阅读"学习Spring Boot 2.0 - 第二版:简化基于微服务和反应式编程的快速闪存应用程序的开发",并且遇到了第一个示例程序之一的问题.
当我http://localhost:8080/chapters对它进行GET 返回时:
[
{},
{},
{}
]
Run Code Online (Sandbox Code Playgroud)
代替:
[
{"id": 1,
"name": "Quick Start with Java"},
{"id":,
"name": "Reactive Web with Spring Boot"},
{"id": 3,
"name": ... and more!"}
]
Run Code Online (Sandbox Code Playgroud)
这是我的代码(减去进口):
@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();
}
}
Run Code Online (Sandbox Code Playgroud)
我的代码有问题吗?
附加信息
如下面的评论所述,我有一点发展.以前我只尝试在我的IDE中运行它.我刚从终端用java -jar build/libs/learning-spring-boot-0.0.1-SNAPSHOT.jar运行它,我得到了正确的响应.适用于失眠,邮差,浏览器和卷曲.这让我可以继续我的工作,但我很乐意为我的IDE解决这个问题.我正在使用Eclipse的Spring Tool套件.
我注意到的一件事是,当我在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
Run Code Online (Sandbox Code Playgroud)
但是当在终端中运行时,我可以看到书名:
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!)
Run Code Online (Sandbox Code Playgroud)
我有同样的问题,对我来说,我添加以确保我的IDE启用了Lombok注释处理(我正在使用IntelliJ Ultimate).启用此功能并重新启动我的应用程序时,我开始按预期查看数据,而不是清空JSON数组.
如前所述在此页面,并适用于你的使用情况:
答案是肯定的.
Flux<Chapter>代表一个流Chapters.但是,默认情况下,它将生成一个JSON数组,因为如果将一个单独的JSON对象流发送到浏览器,那么它将不是一个有效的JSON文档.除了使用Server-Sent-Events或WebSocket之外,浏览器客户端无法使用流.但是,非浏览器客户端可以通过将Accept标头设置为来请求JSON流
application/stream+json,并且响应将是类似于Server-Sent-Events的JSON流,但没有额外的格式:
因此,在您的情况下,您在浏览器中请求结果.如果你想添加适当的accept header,application/stream+json你会得到想要的输出.
| 归档时间: |
|
| 查看次数: |
1283 次 |
| 最近记录: |