David Herod
David Herod

Reputation: 794

How to return a Mono<ResponseEntity> where the response entity can be of two different types

I am new to Spring Webflux / Reactor Core and am trying to perform the following functionality:

  1. call userservice.LoginWebApp()

  2. If a user is returned, return ResponseEntity of type "User". If empty, Return ResponseEntity of type "String"

The following code gives a type error as .defaultIfEmpty() expects ResponseEntity of type user. Can you please advise on the correct operator / method to implement this functionality.

@PostMapping("api/user/login/webApp")
public Mono<ResponseEntity> login(@RequestBody Credentials credentials, ServerWebExchange serverWebExchange) {
     return userService.loginWebApp(credentials, serverWebExchange)
             .map(user -> ResponseEntity.status(HttpStatus.OK).body(user))
             .defaultIfEmpty(ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Invalid username or password"));
}

Upvotes: 4

Views: 36787

Answers (2)

Simon Basl&#233;
Simon Basl&#233;

Reputation: 28341

You can use the cast operator to downcast out of the generic, and I believe WebFlux will still be able to marshal the User and the String:

@PostMapping("api/user/login/webApp")
public Mono<ResponseEntity> login(@RequestBody Credentials credentials, ServerWebExchange serverWebExchange) {
     return userService.loginWebApp(credentials, serverWebExchange)
             .map(user -> ResponseEntity.status(HttpStatus.OK).body(user))
             .cast(ResponseEntity.class)
             .defaultIfEmpty(ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Invalid username or password"));
}

Upvotes: 14

Dmitry
Dmitry

Reputation: 2536

I would do the following:

  1. Make a base class for responses

    abstract class Response {
    }
    
  2. Make separate classes for every kind of response (like UserResponse, ErrorResponse, NotFoundResponse etc) and extend them from the base Response class

    class UserResponse extends Response {
        private String login;
        private String password;
    
        public UserResponse(String login, String password) {
            this.login = login;
            this.password = password;
        }
    
        @JsonGetter("login")
        public String getLogin() {
            return login;
        }
    
        @JsonSetter("login")
        public void setLogin(String login) {
            this.login = login;
        }
    
        @JsonGetter("password")
        public String getPassword() {
            return password;
        }
    
        @JsonSetter("password")
        public void setPassword(String password) {
            this.password = password;
        }
    }
    
    class ErrorResponse extends Response {
        private String errorMessage;
    
        public ErrorResponse(String errorMessage) {
            this.errorMessage = errorMessage;
        }
    
        @JsonGetter("error_message")
        public String getErrorMessage() {
            return errorMessage;
        }
    
        @JsonSetter("error_message")
        public void setErrorMessage(String errorMessage) {
            this.errorMessage = errorMessage;
        }
    }
    
  3. Explicitly set the type of return value Mono<ResponseEntity<Response>> And that's it.

    @GetMapping("/test/{login}")
    public Mono<ResponseEntity<Response>> test(@PathVariable(value = "login") String login) {
        return loginWebApp(login)
                .map(userResponse -> ResponseEntity.status(HttpStatus.OK).body(userResponse))
                .defaultIfEmpty(ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(new ErrorResponse("bad login")));
    }
    

Now let's try it with bad login:

bad login

And good login:

good login

Full code can be found here

Upvotes: 2

Related Questions