I am learning Reactor. I use project-reactor to build a Reactor SpringBoot Demo.I have completed a lot of function and successfully GET/POST in my DEMO.
Now I meet a question, the result return from Controller is not JSON format but a Concatenated String like this: "reactortestPostTitleReactorProgramming Reactor 3.xtestbypostman"
.(I use POSTMAN to test my DEMO)
What I want is a JSON format like this: ["reactortestPostTitle", "ReactorProgramming Reactor 3.x", "testbypostman"]
Now I put my code:
My basic data struction BLOGPOST
defined in Entity package, and using .getTitle()
method could return blog title of String Type :
@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
public class BLOGPOST {
@Id
String id;
String title;
String author;
String body;
}
Model in View, in this class , I use @JsonCreator
and it works:
@Value
@Builder
@AllArgsConstructor(onConstructor = @__(@JsonCreator))
public class PostContent {
@NonNull
String title;
@NonNull
String author;
@NonNull
String body;
}
Controller Code, where I meet question is:
// Get All titles list of Blogs
@GetMapping(value = "/api/blog/alltitles", produces = MediaType.APPLICATION_JSON_VALUE)
public Flux<String> getPostAllTitles() {
return service.getAllTitlesByFlux();
}
Service Class Code, I use JPA repository.findAll()
method to call data from Mysql:
:
public Flux<String> getAllTitlesByFlux(){
return Flux.fromIterable(repository.findAll())
.map(post -> {return post.getTitle();});
}
So, how I can get a JSON format String List by Flux<String> getPostAllTitles()