Spring Boot:创建跨域响应 JSON 的 EmployeeController 及分页方法全实现
好的,下面是一个完整的示例代码,包括 `EmployeeController` 控制器类、分页方法以及统一结果返回的实现。 ### 1. 创建 `EmployeeController` 控制器类 ```java package com.example.demo.controller; import com.example.demo.model.Employee; import com.example.demo.service.EmployeeService; import com.example.demo.util.Result; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; @RestController @RequestMapping("/employees") public class EmployeeController { @Autowired private EmployeeService employeeService; @CrossOrigin(origins = "*") // 解决跨域问题 @PostMapping("/page") public Result getPage(@RequestBody Employee employee, @RequestParam int currentPage, @RequestParam int limit) { return employeeService.findAll(employee, currentPage, limit); } } ``` ### 2. 创建分页方法 在 `EmployeeController` 中已经定义了分页方法 `getPage`,它接收三个参数: - `employee`: 前端传过来的 JSON 数据格式,后端自动转换为对象。 - `currentPage`: 当前页码。 - `limit`: 每页显示的条数。 ### 3. 服务层接口 `EmployeeService` 假设服务层接口 `EmployeeService` 已经存在,并且有一个方法 `findAll`: ```java package com.example.demo.service; import com.example.demo.model.Employee; import com.example.demo.util.Result; public interface EmployeeService { Result findAll(Employee employee, int currentPage, int limit); } ``` ### 4. 统一结果返回类 `Result` 创建一个统一的返回结果类 `Result`: ```java package com.example.demo.util; public class Result<T> { private int code; private String message; private T data; public Result() {} public Result(int code, String message, T data) { this.code = code; this.message = message; this.data = data; } // Getters and Setters public int getCode() { return code; } public void setCode(int code) { this.code = code; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public T getData() { return data; } public void setData(T data) { this.data = data; } // Static method for success response public static <T> Result<T> success(T data) { return new Result<>(200, "Success", data); } // Static method for error response public static <T> Result<T> error(String message) { return new Result<>(500, message, null); } } ``` ### 5. 示例 `Employee` 模型类 为了完整性,这里提供一个简单的 `Employee` 模型类: ```java package com.example.demo.model; public class Employee { private Long id; private String name; private String position; private Double salary; // Getters and Setters public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPosition() { return position; } public void setPosition(String position) { this.position = position; } public Double getSalary() { return salary; } public void setSalary(Double salary) { this.salary = salary; } } ``` 这样,你就完成了一个基本的 Spring Boot 项目,其中包含一个 `EmployeeController` 控制器类,支持分页查询,并能够处理跨域请求和返回统一的结果格式。 ######[AI写代码神器 | 1064点数解答 | 2024-11-28 21:17:59]
- Java 实现计算机硬件模拟:从 PCI 设备到主板、计算机的启动与关闭(字节豆包 | 716点数解答 | 2024-12-08 23:38:38)341
- Java 代码:模拟计算机主板启动与关闭 PCI 设备全流程(字节豆包 | 637点数解答 | 2024-12-09 08:31:54)307
- IntelliJ IDEA 搭建 Spring Boot 项目:从创建到 JPA 实体类及数据源配置全攻略(讯飞星火 | 920点数解答 | 2024-10-31 15:37:22)271
- Spring框架实战:从配置到实现,搞定员工信息查询功能(GPT | 2135点数解答 | 2024-06-12 08:32:35)322
- Spring Boot 与 Vue 联手:轻松实现微信扫码登录全攻略(字节豆包 | 207点数解答 | 2025-05-13 09:12:19)174
- Java 实战:Vehicle 类与 User 类实现机动车速度功率控制( | 968点数解答 | 2024-03-12 11:26:25)556
- Python 实现球类:精准计算半径、表面积与体积,附输入验证与异常处理!(阿里通义 | 261点数解答 | 2024-11-28 21:19:39)362
- Java 创建 HTTP POST 请求实现文件上传:代码示例与注意要点 (字节豆包 | 300点数解答 | 2024-12-06 16:07:20)348
- 超详细:使用Java 8创建连接MySQL数据库的Spring Boot项目全步骤( | 1491点数解答 | 2024-05-13 16:25:58)266
- IDEA搭建 Java 8 连接 MySQL 的 Spring Boot 项目全流程:从创建到测试( | 1985点数解答 | 2024-05-13 16:26:16)306
- 深度解析:Vue + Spring Boot 组合的应用优势与发展前景(字节豆包 | 587点数解答 | 2025-04-29 16:25:49)162
- 揭秘Spring Boot + Vue3前后端分离架构:从开发到安全的全方位技术优势(DeepSeek | 2255点数解答 | 2025-05-07 18:15:21)355