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请求: