- Building RESTful Web Services with Spring 5(Second Edition)
- Raja CSP Raman Ludovic Dewailly
- 358字
- 2021-06-30 19:13:30
getUser – implementation in the handler and repository
Here, we will define and implement the getUser method in our repository. Also, we will call the getUser method in the main class through UserHandler.
We will add an abstract method for the getUser method in the UserRepository class:
Mono<User> getUser(Integer id);
Here, we will add the code for the getUser method. You can see that we have used the Mono return type for single-resource access.
In the UserRepositorySample class (the concrete class for UserRepository), we will implement the abstract method getUser:
@Override
public Mono<User> getUser(Integer id){
return Mono.justOrEmpty(this.users.get(id));
}
In the preceding code, we have retrieved the specific user by id. Also, we have mentioned that if the user is not available, the method should be asked to return an empty Mono.
In the UserHandler method, we will talk about how to handle the request and apply our business logic to get the response:
public Mono<ServerResponse> getUser(ServerRequest request){
int userId = Integer.valueOf(request.pathVariable("id"));
Mono<ServerResponse> notFound = ServerResponse.notFound().build();
Mono<User> userMono = this.userRepository.getUser(userId);
return userMono
.flatMap(user -> ServerResponse.ok().contentType(APPLICATION_JSON).body(fromObject(user)))
.switchIfEmpty(notFound);
}
In the preceding code, we have just converted the string id to an integer in order to supply it to our Repository method (getUser). Once we receive the result from the Repository, we are just mapping it in to Mono<ServerResponse> with the JSON content type. Also, we use switchIfEmpty to send the proper response if no item is available. If the searching item is not available, it will simply return the empty Mono object as a response.
Finally, we will add getUser in our routing path, which is in Server.java:
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)
)
.andRoute(GET("/{id}"), handler::getUser)
);
}
In the preceding code, we have just added a new entry, .andRoute(GET("/{id}"), handler::getUser), in our existing routing path. By doing so, we have added the getUser method and the corresponding REST API part to access a single user. After restarting the server, we should be able to use the REST API.
- Cisco OSPF命令與配置手冊(cè)
- 物聯(lián)網(wǎng)安全(原書第2版)
- 無(wú)人機(jī)通信
- 網(wǎng)絡(luò)安全技術(shù)與解決方案(修訂版)
- 計(jì)算機(jī)網(wǎng)絡(luò)與通信(第2版)
- INSTANT LinkedIn Customization How-to
- 云工廠:開啟中國(guó)制造云時(shí)代
- 互聯(lián)網(wǎng)安全的40個(gè)智慧洞見(2016)
- 基于IPv6的家居物聯(lián)網(wǎng)開發(fā)與應(yīng)用技術(shù)
- NB-IoT原理和優(yōu)化
- 小型局域網(wǎng)組建
- 信息技術(shù)安全評(píng)估準(zhǔn)則:源流、方法與實(shí)踐
- INSTANT Social Media Marketing with HootSuite
- Architecting Data:Intensive Applications
- Hands-On Microservices:Monitoring and Testing