springboot管理系統是一種軟件體系結構樣式,它定義了一組用於創建Web服務的約束。遵循REST體系結構風格的Web服務,稱為RESTful Web服務,提供Internet上計算機系統之間的互操作性。基於rest的Web服務允許請求系統通過使用統一的、預定義的無狀態操作集來訪問和操作Web資源的文本表示。其他類型的Web服務,如SOAP Web服務,公開它們自己的任意操作集。
springboot后台權限管理系統及文件博客系統源碼:s.ymzan.top
在本文中,我們將了解如何使用rest API創建springboot后台管理系統。

Spring被廣泛用於創建可伸縮的應用程序。對於web應用,Spring提供了Spring MVC,這是Spring中被廣泛使用的模塊,用於創建可伸縮的web應用。但是spring項目的主要缺點是配置非常耗時,並且對新開發人員來說可能有點難以承受。解決方案是Spring Boot。Spring Boot構建在彈簧的頂部,包含彈簧的所有特性。在本文中,我們將創建一個REST API來將員工添加到員工列表並獲得員工列表。為了做到這一點,我們首先必須在任何IDE中創建一個簡單的Spring Boot項目,並遵循以下步驟:
1、最初,我們需要定義員工實體。因此,定義了以下employee類:
package com.example.demo; // Creating an entity Employee public class Employee { public Employee() {} // Parameterized Constructor // to assign the values // to the properties of // the entity public Employee( Integer id, String firstName, String lastName, String email) { super(); this.id = id; this.firstName = firstName; this.lastName = lastName; this.email = email; } private Integer id; private String firstName; private String lastName; private String email; // Overriding the toString method // to find all the values @Override public String toString() { return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", email=" + email + "]"; } // Getters and setters of // the properties public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getFirstName() { return firstName; } public void setFirstName( String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName( String lastName) { this.lastName = lastName; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } }
2、現在,我們需要創建一個存儲類,存儲所有員工的列表:
package com.example.demo; import java.util.ArrayList; import java.util.List; // Class to store the list of // all the employees in an // Array List public class Employees { private List<Employee> employeeList; // Method to return the list // of employees public List<Employee> getEmployeeList() { if (employeeList == null) { employeeList = new ArrayList<>(); } return employeeList; } public void setEmployeeList( List<Employee> employeeList) { this.employeeList = employeeList; } }
3、到目前為止,我們已經定義了實體雇員並創建了一個存儲類。現在,我們需要接觸員工。因此,我們創建了一個類,在這個類中我們將創建一個存儲類的對象來存儲雇員:
package com.example.demo; import org.springframework .stereotype .Repository; // Importing the employees class to // use the defined properties // in this class import com.example.demo.Employees; @Repository // Class to create a list // of employees public class EmployeeDAO { private static Employees list = new Employees(); // This static block is executed // before executing the main // block static { // Creating a few employees // and adding them to the list list.getEmployeeList().add( new Employee( 1, "Prem", "Tiwari", "chapradreams@gmail.com")); list.getEmployeeList().add( new Employee( 2, "Vikash", "Kumar", "abc@gmail.com")); list.getEmployeeList().add( new Employee( 3, "Ritesh", "Ojha", "asdjf@gmail.com")); } // Method to return the list public Employees getAllEmployees() { return list; } // Method to add an employee // to the employees list public void addEmployee(Employee employee) { list.getEmployeeList() .add(employee); } }
4、最后,我們需要創建一個控制器類,它是REST API的實際實現。根據REST規則,數據庫中的每個新條目必須由POST方法調用,數據庫中的所有請求必須使用GET方法調用。下面的代碼實現了相同的方法:
package com.example.demo; import java.net.URI; import org.springframework.beans .factory.annotation.Autowired; import org.springframework.http .ResponseEntity; import org.springframework.web.bind .annotation.GetMapping; import org.springframework.web.bind .annotation.PostMapping; import org.springframework.web.bind .annotation.RequestBody; import org.springframework.web.bind .annotation.RequestMapping; import org.springframework.web.bind .annotation.RestController; import org.springframework.web.servlet .support.ServletUriComponentsBuilder; // Import the above-defined classes // to use the properties of those // classes import com.example.demo.Employees; import com.example.demo.EmployeeDAO; import com.example.demo.Employee; // Creating the REST controller @RestController @RequestMapping(path = "/employees") public class EmployeeController { @Autowired private EmployeeDAO employeeDao; // Implementing a GET method // to get the list of all // the employees @GetMapping( path = "/", produces = "application/json") public Employees getEmployees() { return employeeDao .getAllEmployees(); } // Create a POST method // to add an employee // to the list @PostMapping( path = "/", consumes = "application/json", produces = "application/json") public ResponseEntity<Object> addEmployee( @RequestBody Employee employee) { // Creating an ID of an employee // from the number of employees Integer id = employeeDao .getAllEmployees() .getEmployeeList() .size() + 1; employee.setId(id); employeeDao .addEmployee(employee); URI location = ServletUriComponentsBuilder .fromCurrentRequest() .path("/{id}") .buildAndExpand( employee.getId()) .toUri(); return ResponseEntity .created(location) .build(); } }
5、在實現項目中的所有類之后,以彈簧啟動應用程序。一旦服務器開始運行,我們就可以通過瀏覽器或郵遞員發送請求。我們可以通過以下URL訪問正在運行的應用程序:
localhost:8080/employees/
以下是運行上述項目時產生的輸出:
當執行GET請求時:

當POST請求被執行時:

再次在執行POST請求后碰到GET請求:

