[英]ClassLoader.getResources returning an empty Enumeration on a Weblogic/Spring application
[英]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
来请求 JSONapplication/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.