[英]Flux returns empty objects
I am starting with Flux today for it is pretty powerful. 我今天从Flux开始,因为它功能强大。 Now I have set up a whole simple Spring boot 2 project to work with this but the returned objects are empty. 现在,我已经建立了一个完整的Spring Boot 2项目来处理此问题,但是返回的对象为空。
I started a very simple Spring Boot project with some dependencies: 我开始了一个非常简单的带有某些依赖项的Spring Boot项目:
And added some code: 并添加了一些代码:
controller: 控制器:
@RestController
public class ChapterController {
@Autowired
private ChapterRepository repository;
@GetMapping("/chapters")
public Flux<Chapter> listing() {
return repository.findAll();
}
}
Repository: 库:
public interface ChapterRepository extends ReactiveCrudRepository<Chapter, String> {}
Configuration: (to load some data in the embeded Mongodb) @Configuration public class LoadDatabase { 配置:(以在嵌入的Mongodb中加载一些数据) @Configuration公共类LoadDatabase {
@Bean
CommandLineRunner init(ChapterRepository repository){
return args -> {
Flux.just(
new Chapter("The life of Batman"),
new Chapter("Batmans most glorious' code"),
new Chapter("The hero we needed but didn't deserve, Batman."))
.flatMap(repository::save)
.subscribe(System.out::println);
};
}
}
Data class: 资料类别:
@Data
@Document
public class Chapter {
@Id
private String id;
private String name;
public Chapter(String name) {
this.name = name;
}
}
Ok, now when I start the application and access the endpoint: http://localhost:8080/chapters it returns this: 好的,现在当我启动应用程序并访问端点: http:// localhost:8080 / chapters时,它将返回以下内容:
[
{},
{},
{}
]
It is displaying the same amount of objects that I created in the LoadDatabase
class. 它显示与在LoadDatabase
类中创建的对象数量相同的对象。 When I change the amount of objects created, it shows that amount on the endpoint. 当我更改创建的对象数量时,它将在端点上显示该数量。
I don't know what I did wrong, I tried debugging the flux object returned. 我不知道自己做错了什么,我尝试调试返回的助焊剂对象。 But I can't make anything out of it. 但是我不能从中得到任何东西。
I hope someone can spot my mistake! 我希望有人能发现我的错误!
You are getting empty objects because the data is not saved and something went wrong. 您正在获取空对象,因为未保存数据并且出了点问题。
You are using @Data
lombok annotation which is like having implicit @Getter, @Setter, @ToString, @EqualsAndHashCode and @RequiredArgsConstructor annotations on the class (except that no constructor will be generated if any explicitly written constructor exists). 您正在使用@Data
lombok注释,就像在类上具有隐式@ Getter,@ Setter,@ ToString,@ EqualsAndHashCode和@RequiredArgsConstructor注释一样(除非存在任何显式编写的构造函数,否则不会生成任何构造函数)。 but sometimes it doesn't work if not configured in IDE correctly so try once with manual getters and setters for the properties. 但有时,如果未在IDE中正确配置,它将无法正常工作,因此请尝试使用手动getter和setter方法设置属性。
If the manual getters/setters work then try below to troubleshoot for lombok. 如果手动吸气器/安装器正常工作,请尝试以下操作以对Lombok进行故障排除。
Ensure that your IDE is aware of lombok. 确保您的IDE知道lombok。
IntelliJ : Lombok added but getters and setters not recognized in Intellij IDEA IntelliJ: 添加了Lombok,但是在Intellij IDEA中无法识别吸气剂和吸气剂
Eclipse : Lombok is not generating getter and setter Eclipse: Lombok不会生成getter和setter
If the problem still exists then follow this similar thread's one of the comments here 如果问题仍然存在,那么按照此类似线程的一个评论点击这里
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.