1

I am unable to return Flux<T> in ResponseEntity. In my repository I return Flux<Student>. Now I want to handle this response in controller with switchIfEmpty to handle 404 error code using switchIfEmpty. Below is what I tried:

repository

public interface StudentRepository extends R2dbcRepository<Student, Integer>{

    Flux<Student> findByFirstName(String firstName);
    
}

service

public Flux<Student> getStudentByName(String name){
        return repo.findByFirstName(name);
    }

Controller (this is where I am not able produce response)

@GetMapping(value="student/name/{name}")
    public Flux<ResponseEntity<Student>> getStudentByName(@PathVariable(name = "name",required = true)String name) {
        Flux<ResponseEntity<Student>> map = service.getStudentByName(name)
                .switchIfEmpty(Mono.error(new NoSuchElementException("student not found"))) 
                .map(stu->new ResponseEntity<>(Mono.just(stu),HttpStatus.OK));
    return map;
    }

As I was getting Only a single ResponseEntity supported I tried with below code:

@GetMapping(value="student/name/{name}")
    public Flux<ResponseEntity<Student>> getStudentByName(@PathVariable(name = "name",required = true)String name) {
         Flux<ResponseEntity<Student>> flatMap = service.getStudentByName(name)
        .switchIfEmpty(Mono.error(new NoSuchElementException("student not found"))) 
        .flatMap(stu->Flux.just(new ResponseEntity<Student>(stu,HttpStatus.OK)));
        return flatMap;
    }

I am still getting same exception i.e. Only a single ResponseEntity supported

I have handled NoSuchElementException as global exception.

How do I return Flux<Student> wrapped inside ResponseEntity.. or vice versa as ResponseEntity wrapped inside Flux. What approach should I go with to return Flux object.

2
  • Best to avoid using spring webflux. Commented Jun 9 at 11:35
  • 1
    A Flux<ResponseEntity<Student> doesn't really make sense a http response can only have a single status not multiple. You should have a ResponseEntity<Flux<Student>>. Next to that what you are returning isn't a Flux<ResponseEntity<Student>> either, looking at your mapping it is more of a Flux<ResponseEntity<Mono<Student>>>. Which is even more illogical. Commented Jun 9 at 15:19

2 Answers 2

0

below code resolved my problem.

    @GetMapping(value="student/name/{name}",produces = "text/event-stream")
    public ResponseEntity<Flux<Student>> getStudentByName(@PathVariable(name = "name",required = true)String name) {
        Flux<Student> flatMap2 = service.getStudentByName(name)
                .switchIfEmpty(Mono.error(new NoSuchElementException("student not found"))) 
                .flatMap(stu->Flux.just(stu));

        return new ResponseEntity<Flux<Student>>(flatMap2,HttpStatus.OK);
    }
Sign up to request clarification or add additional context in comments.

Comments

0

You should be able to return a Flux<Student> directly just as you did in the service, without wrapping it in a ResponseEntity, more so because you hardcoded the status code. That said, returning HTTP 200 when the resource is not found is a fly in the face of REST.

4 Comments

Mr. Sarkar, actually when the resource is not found, I am checking it inside switchIfEmpty and redirecting it to global exception handling for 404 status. Also if any other exception occurs. But you have raised a good question on how to handle errors in reactive, means there is something onError. How to use that with REST.
if you are returning a Flux<Student> you are implicitly stating that there could be multiple Students returned. Depending on the mediatype set. if mediatype header is set to application/json webflux will buffer the returned students and when all are buffered it will return a list of students. if set to text/event-stream server will return students on by one as a server sent event (SSE stream). And if application/stream+json it will stream new line delimited json objects.
Returning 404 tells the calling client that the endpoint called does not exist. Meaning, If you insist on return a flux with students, you should returning a empty list if someone asks for students that dont exist. Not 404
ok @Toerktumlare I noted your both points. Also I am seeing some more approaches of handling exceptions discussed here stackoverflow.com/a/53596358/3422726

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.