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

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.

主站蜘蛛池模板: 凤城市| 安义县| 清流县| 京山县| 建湖县| 越西县| 东莞市| 石台县| 榕江县| 巨鹿县| 利辛县| 长武县| 义乌市| 白玉县| 肃宁县| 大田县| 罗平县| 临清市| 平南县| 贡山| 安丘市| 广宁县| 扎赉特旗| 大余县| 清远市| 沈丘县| 乐山市| 山阳县| 扎囊县| 巴林右旗| 开平市| 都江堰市| 岫岩| 承德县| 凤台县| 营山县| 始兴县| 大同市| 肃宁县| 高密市| 建宁县|