Flux returns 个空对象
Flux returns empty objects
我今天开始使用 Flux,因为它非常强大。现在我已经建立了一个完整的简单 Spring boot 2 项目来处理它,但返回的对象是空的。
我启动了一个非常简单的 Spring 引导项目,其中包含一些依赖项:
- 响应式网络(嵌入式 Netty + Spring WebFlux)
- 反应性MongoDB(Spring数据MongoDB)
- Thymeleaf 模板引擎
- Lombok(简化编写 POJO)
并添加了一些代码:
控制器:
@RestController
public class ChapterController {
@Autowired
private ChapterRepository repository;
@GetMapping("/chapters")
public Flux<Chapter> listing() {
return repository.findAll();
}
}
存储库:
public interface ChapterRepository extends ReactiveCrudRepository<Chapter, String> {}
配置:(在嵌入的Mongodb中加载一些数据)
@配置
public class 加载数据库 {
@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);
};
}
}
数据class:
@Data
@Document
public class Chapter {
@Id
private String id;
private String name;
public Chapter(String name) {
this.name = name;
}
}
好的,现在当我启动应用程序并访问端点时:http://localhost:8080/chapters 它 returns 这个:
[
{},
{},
{}
]
它显示的对象数量与我在 LoadDatabase
class 中创建的对象数量相同。当我更改创建的对象数量时,它会在端点上显示该数量。
我不知道我做错了什么,我尝试调试返回的通量对象。但我无法从中得到任何东西。
希望有人能指出我的错误!
您正在获取空对象,因为数据未保存且出现问题。
您正在使用 @Data
lombok 注释,这就像在 class 上隐含 @Getter、@Setter、@ToString、@EqualsAndHashCode 和 @RequiredArgsConstructor (除非存在任何显式编写的构造函数,否则不会生成构造函数)。但有时如果未在 IDE 中正确配置,它就不起作用,因此请尝试使用属性的手动 getter 和 setter 一次。
如果手册 getters/setters 有效,请尝试下面的 lombok 故障排除。
确保您的 IDE 知道 lombok。
IntelliJ:Lombok added but getters and setters not recognized in Intellij IDEA
日食:Lombok is not generating getter and setter
如果问题仍然存在,请关注此类似主题的评论之一 here
我今天开始使用 Flux,因为它非常强大。现在我已经建立了一个完整的简单 Spring boot 2 项目来处理它,但返回的对象是空的。
我启动了一个非常简单的 Spring 引导项目,其中包含一些依赖项:
- 响应式网络(嵌入式 Netty + Spring WebFlux)
- 反应性MongoDB(Spring数据MongoDB)
- Thymeleaf 模板引擎
- Lombok(简化编写 POJO)
并添加了一些代码:
控制器:
@RestController
public class ChapterController {
@Autowired
private ChapterRepository repository;
@GetMapping("/chapters")
public Flux<Chapter> listing() {
return repository.findAll();
}
}
存储库:
public interface ChapterRepository extends ReactiveCrudRepository<Chapter, String> {}
配置:(在嵌入的Mongodb中加载一些数据) @配置 public class 加载数据库 {
@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);
};
}
}
数据class:
@Data
@Document
public class Chapter {
@Id
private String id;
private String name;
public Chapter(String name) {
this.name = name;
}
}
好的,现在当我启动应用程序并访问端点时:http://localhost:8080/chapters 它 returns 这个:
[
{},
{},
{}
]
它显示的对象数量与我在 LoadDatabase
class 中创建的对象数量相同。当我更改创建的对象数量时,它会在端点上显示该数量。
我不知道我做错了什么,我尝试调试返回的通量对象。但我无法从中得到任何东西。
希望有人能指出我的错误!
您正在获取空对象,因为数据未保存且出现问题。
您正在使用 @Data
lombok 注释,这就像在 class 上隐含 @Getter、@Setter、@ToString、@EqualsAndHashCode 和 @RequiredArgsConstructor (除非存在任何显式编写的构造函数,否则不会生成构造函数)。但有时如果未在 IDE 中正确配置,它就不起作用,因此请尝试使用属性的手动 getter 和 setter 一次。
如果手册 getters/setters 有效,请尝试下面的 lombok 故障排除。
确保您的 IDE 知道 lombok。
IntelliJ:Lombok added but getters and setters not recognized in Intellij IDEA
日食:Lombok is not generating getter and setter
如果问题仍然存在,请关注此类似主题的评论之一 here