Building RESTful Web Services with Spring 5(Second Edition)
上QQ阅读APP看书,第一时间看更新

getAllUsers – implementation in the handler and repository

Here, we will define and implement the getAllUsers method in our repository. Also, we will call the getAllUsers method in the main class through UserHandler.

We will add an abstract method for the getAllUsers method in the UserRepository class:

Flux<User> getAllUsers();

Like any other interface and concrete class implementation, we will have to add the abstract method in our interface, in our case, UserRespository. The preceding code just adds getAllUsers in the UserRepository class.

In UserRepositorySample (the concrete class for UserRepository), we will implement the abstract method getAllUsers:

// this method will return all users
@Override
public Flux<User> getAllUsers() {
return Flux.fromIterable(this.users.values());
}

In the preceding code, we have added the method getAllUsers and implemented the business logic. As we have already defined the users in the UserRepositorySample constructor, we just need to return the users. The Flux class has a method called fromIterable, which is used to get all users from our UserRepositorySample.

The fromIterable method will return a Flux that emits the items contained in our Java Collection interface. As Collection implements iterable interface, fromIterable will be the perfect method to return Flux in our case.

In the UserHandler.java file, we will add the code to get all users in Reactive. The following code will walk us through the  necessary details:

public Mono<ServerResponse> getAllUsers(ServerRequest request){
Flux<User> users = this.userRepository.getAllUsers();
return ServerResponse.ok().contentType(APPLICATION_JSON).body(users, User.class);
}

In the preceding code, we will get all users from the repository in Flux and we will send them in the response in the JSON type. The server response content type is updated with APPLICATION_JSON

Now is the time to add our first method, getAllUsers, in our routing method. Here, we will use only one routing method to map all REST APIs. 

Finally, our routing function will look as follows in Server.java:

public class Server {    
// existing code is hidden
public RouterFunction<ServerResponse> routingFunction() {
UserRepository repository = new UserRepositorySample();
UserHandler handler = new UserHandler(repository);
return nest (
path("/user"),
nest(
accept(MediaType.ALL),
route(GET("/"), handler::getAllUsers)
)
);
}

In the preceding code, we created a UserRepository and forwarded it to our UserHandler. UserHandler will automatically call the getAllUsers method in UserSampleRepository. By calling the getAllUsers method of UserHandler, we will get all users from the sample repository class that we have implemented before.

Here, we are using the nest method and supplying parameters, such as the API path GET("/") and the media type. As the nest method accepts RoutingFunction as the second parameter, we can use more nest methods inside our basic nest methods. By using inner nesting methods, we have achieved the business requirement: our basic REST API starts from "/user" and basic get users API routing by "/"

So, the basic API path /user will automatically call the getAllUsers method as it's implemented in the preceding code.