【发布时间】:2020-07-29 08:52:54
【问题描述】:
我正在学习 Reactor。我使用project-reactor搭建了一个Reactor SpringBoot Demo。我已经完成了很多功能,并且在我的DEMO中成功GET/POST。
现在我遇到一个问题,Controller 返回的结果不是 JSON 格式,而是这样的连接字符串:"reactortestPostTitleReactorProgramming Reactor 3.xtestbypostman"。(我使用 POSTMAN 测试我的 DEMO)
我想要的是这样的 JSON 格式:["reactortestPostTitle", "ReactorProgramming Reactor 3.x", "testbypostman"]
现在我输入我的代码:
我在Entity包中定义的基本数据结构BLOGPOST,使用.getTitle()方法可以返回String类型的博客标题:
@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
public class BLOGPOST {
@Id
String id;
String title;
String author;
String body;
}
View 中的模型,在这个类中,我使用 @JsonCreator 并且它有效:
@Value
@Builder
@AllArgsConstructor(onConstructor = @__(@JsonCreator))
public class PostContent {
@NonNull
String title;
@NonNull
String author;
@NonNull
String body;
}
控制器代码,我遇到的问题是:
// Get All titles list of Blogs
@GetMapping(value = "/api/blog/alltitles", produces = MediaType.APPLICATION_JSON_VALUE)
public Flux<String> getPostAllTitles() {
return service.getAllTitlesByFlux();
}
服务类代码,我使用JPArepository.findAll()方法从Mysql调用数据:
:
public Flux<String> getAllTitlesByFlux(){
return Flux.fromIterable(repository.findAll())
.map(post -> {return post.getTitle();});
}
那么,我如何通过Flux<String> getPostAllTitles()获得JSON格式的字符串列表
【问题讨论】:
标签: json spring-boot project-reactor restful-url