- 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)
);
}
推薦閱讀
- 物聯網短距離無線通信技術應用與開發
- 物聯網(IoT)基礎:網絡技術+協議+用例
- C++黑客編程揭秘與防范
- Building Django 2.0 Web Applications
- Twilio Cookbook
- GPS/GNSS原理與應用(第3版)
- 工業控制網絡安全技術與實踐
- Spring Boot 2.0 Projects
- Force.com Development Blueprints
- 正在爆發的互聯網革命
- Yii Application Development Cookbook(Second Edition)
- Microservice Patterns and Best Practices
- WordPress Web Application Development
- OMNeT++與網絡仿真
- 網管第一課:網絡操作系統與配置管理