I am new to reactive programming. To get my hands on i am trying to build a simple rest api but with request validation and db operations.
here are my steps i would like to do.
- validate incoming request parameter
- after verified keep continue chain and fetch from db
- If user not exists in db return some error response
- If user exists return success response with user name
note: request and response represented by classes (UserRequest and UserResponse). DB = Mongo using reactive drivers.
I have done the validation work but now i am confuse how to continue and what is the proper way in reactive programming.
@Component
public class UserController {
@Autowired
private UserRepository userRepository; // repo has find by id method which returns Mono<User>
public Mono<ServerResponse> handleUserRequest(ServerRequest serverRequest) {
Mono<UserRequest> request = validateRequest(serverRequest);
// what should i do here like now i would like to fetch user from db
return ServerResponse.ok().body("Welcome", String.class);
// how would i return "Welcome <username>";
}
private Mono<UserRequest> validateRequest(ServerRequest request) {
Mono<UserRequest> userRequest = Mono.just(new UserRequest());
Mono<UserRequest> validateUser = userRequest
.map(req -> { //validate id
Optional<String> userid = request.queryParam("userid");
if (user.isPresent() && ObjectId.isValid(userid.get())) {
return req.setUserid(userid.get());
}
throw new RequestEntityValidationException("Invalid user");
});
return validateUser;
}
}
Also is there any side by side tutorial like which show general code snippets in imperative then in reactive.