- 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)
);
}
推薦閱讀
- Application Development with Qt Creator(Second Edition)
- 重新定義Spring Cloud實(shí)戰(zhàn)
- 計(jì)算機(jī)網(wǎng)絡(luò)與數(shù)據(jù)通信
- 新一代物聯(lián)網(wǎng)架構(gòu)技術(shù):分層算力網(wǎng)絡(luò)
- Go Web Scraping Quick Start Guide
- 計(jì)算機(jī)網(wǎng)絡(luò)工程實(shí)用教程(第2版)
- 物聯(lián)網(wǎng)與無(wú)線傳感器網(wǎng)絡(luò)
- 物聯(lián)網(wǎng)通信技術(shù)
- Metasploit Penetration Testing Cookbook
- 網(wǎng)管員必讀:網(wǎng)絡(luò)管理(第2版)
- C/C++串口通信:典型應(yīng)用實(shí)例編程實(shí)踐
- 基于性能的保障理論與方法
- 通信十年:擁抱互聯(lián)網(wǎng)
- 物聯(lián)網(wǎng)工程概論
- AIoT應(yīng)用開(kāi)發(fā)與實(shí)踐