How to get username from mono<user> on spring boot webflux?
Asked Answered
Q

2

3

I try to make handler and router classes of spring boot webflux. The model class is user class. Codes are

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Document(collection="Users") 
public class User {

    @Id 
    private String _id;

    @Indexed(unique = true) 
    private Long id; 

    @Indexed(unique=true)  
    private String username;

    private String password;

    private String email;

    private String fullname;

    private String role;
}

And below is the handler class of webflux project. In register method, I make the id duplication test codes. But It is totally WRONG.

@Component
public class UserHandler {

    @Autowired
    private UserReactiveMongoRepository userRepository;

    public Mono<ServerResponse> register(ServerRequest request) {
        Mono<User> monoUser = request.bodyToMono(User.class);
        String id = monoUser.map(u -> u.get_id()).toString();

        if(userRepository.existsById(id) == null)
            return ServerResponse.ok().build(userRepository.insert(monoUser));

        return ServerResponse.ok().build();
    }
}

I want to extract the username or id string from Mono of spring webflux. Any comments will be needed. I am stuck with this part.

Quaff answered 1/7, 2019 at 5:55 Comment(0)
S
5

One of the things which is wrong here is this like String id = monoUser.map(u -> u.get_id()).toString();. The toString will return you a String like "Mono@13254216541", because you are calling Mono.toString.

One more thing, you shouldn't use the request's data in the body of your function, but in a map or flatMap function.

You could replace it by something like (I'm doing it by head so it might not be 100% syntax corect):

Mono<User> userMono = request.bodyToMono(User.class);//Create a Mono<User>

userMono.map((user) -> { //In the map method, we access to the User object directly
  if(user != null && user.getId() != null){
    return userRepository.insert(user); // Insert User instead of Mono<User> in your repository
  }
  return null;
}) //This is still a Mono<User>
.map(insertedUser -> ServerResponse.ok(insertedUser)) //This is a Mono<ServerResponse>
.switchIfEmpty(ServerResponse.ok());

Hope this helps!

Standice answered 1/7, 2019 at 8:34 Comment(0)
C
1

a more clean approach would be (i don't like the return of null in the map that others have done) is use the doOnSuccess:

request.bodyToMono(User.class)
    .doOnSuccess(user -> return userRepository.insert(user))
    .map(user -> ServerResponse.ok(user))

i have left out any error checks but ofc they should be done properly.

Candleberry answered 1/7, 2019 at 11:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.