2

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()

dyy.alex
  • 514
  • 2
  • 4
  • 16

1 Answers1

4

Take a look at this answer. This is what going on in your case. You can use the provided solution there.


Easy solution: You can simply change the Flux<String> to Flux<Object>

@GetMapping("/api/blog/alltitles")
public Flux<Object> getPostAllTitles() {
    return service.getAllTitlesByFlux();
}

Another solution: If you do not want to go with the above 2 approaches, then, You are looking for List<String> and based on your requirement, your return type should be Mono<List<String>>. You can take a look at this collectList method.

// Get All titles list of Blogs
@GetMapping(value = "/api/blog/alltitles")
public Mono<List<String>> getPostAllTitles() {
    return service.getAllTitlesByFlux()
                  .take(3)
                  .collectList();
}
vins
  • 15,030
  • 3
  • 36
  • 47
  • Thanks for your link. Your link help me a lot. And more, I tried Your 2 solution. The first solution doesnot work, maybe flux doesnot support it; The second is not reactor style but works. Finally, my success solution is using `Flux`, Spring Boot will not recognize as raw JSON string. And the links is in here[1]. [1] https://stackoverflow.com/questions/44317740/there-is-a-clean-way-to-return-string-as-json-in-a-spring-web-api?noredirect=1&lq=1 – dyy.alex Jul 30 '20 at 01:43
  • You mean `Flux` does not work? I did test before answering. – vins Jul 30 '20 at 01:52
  • The second one you said, ty. I don't think the first one is an option. – Marco Sulla Jun 16 '23 at 08:12