- Building RESTful Web Services with Spring 5(Second Edition)
- Raja CSP Raman Ludovic Dewailly
- 237字
- 2021-06-30 19:13:33
CRUD operation in Spring 5 (without Reactive)
Let's perform user CRUD operations. As we have discussed CRUD concepts before, here we will only discuss User management on Spring 5 (without Reactive support). Let's fill in all dummy methods for CRUD endpoints. In here, we can create UserContoller and fill in all methods for CRUD user operations:
package com.packtpub.restapp;
import java.util.LinkedHashMap;
import java.util.Map;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/user")
public class UserController {
@ResponseBody
@RequestMapping("")
public Map<String, Object> getAllUsers(){
Map<String, Object> map = new LinkedHashMap<>();
map.put("result", "Get All Users Implementation");
return map;
}
@ResponseBody
@RequestMapping("/{id}")
public Map<String, Object> getUser(@PathVariable("id") Integer id){
Map<String, Object> map = new LinkedHashMap<>();
map.put("result", "Get User Implementation");
return map;
}
@ResponseBody
@RequestMapping(value = "", method = RequestMethod.POST)
public Map<String, Object> createUser(){
Map<String, Object> map = new LinkedHashMap<>();
map.put("result", "Create User Implementation");
return map;
}
@ResponseBody
@RequestMapping(value = "", method = RequestMethod.PUT)
public Map<String, Object> updateUser(){
Map<String, Object> map = new LinkedHashMap<>();
map.put("result", "Update User Implementation");
return map;
}
@ResponseBody
@RequestMapping(value = "", method = RequestMethod.DELETE)
public Map<String, Object> deleteUser(){
Map<String, Object> map = new LinkedHashMap<>();
map.put("result", "Delete User Implementation");
return map;
}
}
We have filled the basic endpoints for all CRUD operations. If you call them on Postman with proper methods such as GET, POST, PUT, and DELETE, you will see the result mentioning the appropriate messages.
For example, for the getAllUsers API (localhost:8080/user), you will get:
{
result: "Get All Users Implementation"
}
推薦閱讀
- EJB 3.1從入門到精通
- 網(wǎng)絡(luò)云百問百答
- Truffle Quick Start Guide
- 5G承載網(wǎng)網(wǎng)絡(luò)規(guī)劃與組網(wǎng)設(shè)計
- 物聯(lián)網(wǎng)信息安全
- 無人機通信
- Building RESTful Web Services with Spring 5(Second Edition)
- 物聯(lián)網(wǎng)與無線傳感器網(wǎng)絡(luò)
- Learning Swift(Second Edition)
- WordPress Web Application Development
- Android UI Design
- 大型企業(yè)微服務(wù)架構(gòu)實踐與運營
- 云計算技術(shù)與標準化
- 計算機網(wǎng)絡(luò)技術(shù)
- 圖解物聯(lián)網(wǎng)