官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 河间市| 泾阳县| 黄浦区| 漠河县| 老河口市| 绥宁县| 上饶县| 正阳县| 五莲县| 启东市| 岳阳市| 邢台市| 镇巴县| 浦江县| 石柱| 从化市| 顺义区| 泗水县| 仁布县| 富锦市| 盐源县| 满洲里市| 龙门县| 湘阴县| 黄石市| 余江县| 孝感市| 巴南区| 右玉县| 锡林浩特市| 莆田市| 南通市| 中西区| 温州市| 南汇区| 金寨县| 盐城市| 阜新| 八宿县| 郑州市| 宜州市|