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

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.

主站蜘蛛池模板: 井冈山市| 吴忠市| 许昌市| 方正县| 定日县| 新宾| 鄂托克旗| 忻州市| 沭阳县| 永春县| 闵行区| 泗阳县| 驻马店市| 邛崃市| 韩城市| 读书| 阳高县| 晋城| 张北县| 乳源| 穆棱市| 图木舒克市| 忻城县| 刚察县| 体育| 泸定县| 延津县| 虹口区| 威海市| 会宁县| 高雄县| 常宁市| 大安市| 拉萨市| 新乡市| 曲沃县| 望城县| 洛阳市| 天等县| 浑源县| 三江|