- Building RESTful Web Services with Spring 5(Second Edition)
- Raja CSP Raman Ludovic Dewailly
- 193字
- 2021-06-30 19:13:32
deleteUser – implementation in the handler and repository
Here, we will define and implement the deleteUser method in our repository. Also, we will call the deleteUser method in the main class through UserHandler.
As usual, we will add an abstract method for the deleteUser method in the UserRepository class:
Mono<Void> deleteUser(Integer id);
In the UserRepositorySample.java file, we will add the deleteUser method to remove the specified user from the list:
@Override
public Mono<Void> deleteUser(Integer id) {
users.remove(id);
System.out.println("user : "+users);
return Mono.empty();
}
In the preceding method, we simply remove the element from users and return an empty Mono object.
As we have added the deleteUser method in the repository, here we will follow up on our handler:
public Mono<ServerResponse> deleteUser(ServerRequest request) {
int userId = Integer.valueOf(request.pathVariable("id"));
return ServerResponse.ok().build(this.userRepository.deleteUser(userId));
}
Finally, we will add the REST API path to save the user in our existing routing function 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)
.andRoute(POST("/").and(contentType(APPLICATION_JSON)), handler::createUser)
.andRoute(PUT("/").and(contentType(APPLICATION_JSON)), handler::updateUser)
.andRoute(DELETE("/{id}"), handler::deleteUser)
);
}
推薦閱讀
- 網(wǎng)管員典藏書架:網(wǎng)絡(luò)管理與運維實戰(zhàn)寶典
- 物聯(lián)網(wǎng)網(wǎng)絡(luò)安全及應(yīng)用
- 物聯(lián)網(wǎng)識別技術(shù)
- Learning Karaf Cellar
- PLC、現(xiàn)場總線及工業(yè)網(wǎng)絡(luò)實用技術(shù)速成
- Mastering Dart
- Kong網(wǎng)關(guān):入門、實戰(zhàn)與進階
- Unity Artificial Intelligence Programming
- The Kubernetes Workshop
- 6G:面向2030年的移動通信
- 端到端QoS網(wǎng)絡(luò)設(shè)計
- 人人都該都懂的互聯(lián)網(wǎng)思維
- Guide to NoSQL with Azure Cosmos DB
- 現(xiàn)場綜合化網(wǎng)絡(luò)運營與維護:運營商數(shù)字化轉(zhuǎn)型技術(shù)與實踐
- Building Microservices with Spring