【发布时间】:2021-09-16 06:53:20
【问题描述】:
我是 java 和 springboot 的新手。我正在尝试使用 springboot 创建一个 CRUD 应用程序。 我正在使用 MySQL 来存储数据。
员工模型 -
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "employees")
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
@Column(name = "email_id")
private String emailId;
public Employee() {
}
public Employee(String firstName, String lastName, String emailId) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.emailId = emailId;
}
public long getId() {
return id;
}
public void setId(long 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 getEmailId() {
return emailId;
}
public void setEmailId(String emailId) {
this.emailId = emailId;
}
}
员工资料库 -
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.raksh.springboot.model.Employee;
@Repository
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
}
员工控制器 -
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.raksh.springboot.model.Employee;
import com.raksh.springboot.repository.EmployeeRepository;
@CrossOrigin(origins = "http://localhost:3000/")
@RestController
@RequestMapping("/api/v1/")
public class EmployeeController {
@Autowired
private EmployeeRepository employeeRepository;
// get all employees
@GetMapping("/employees")
public List<Employee> getAllEmployees(){
return employeeRepository.findAll();
}
}
上面的控制器以 JSON 对象数组形式给我结果,如下所示
[
{
"id": 1,
"firstName": "Tony",
"lastName": "Stark",
"emailId": "tony@gmail.com"
},
{
"id": 2,
"firstName": "Thor",
"lastName": "Odinson",
"emailId": "thor@asgard.com"
}
]
但我需要以下表格的回复
{
total_items: 100,
has_more: true,
employees : {
1 : {
"id": 1,
"firstName": "Raksh",
"lastName": "Sindhe",
"emailId": "raksh@gmail.com"
},
2: {
"id": 2,
"firstName": "Thor",
"lastName": "Odinson",
"emailId": "thor@asgard.com"
}
}
}
非常感谢您的帮助。
【问题讨论】:
-
要么您需要将 total_items 和 has_more 字段添加到员工类和您的 mysql 表中,或者在从控制器返回之前,您可以通过创建一个自定义类来修改您的对象,该类在获取后获取 total_items、has_more 和员工列表员工,您可以设置 total_items 和 has_more 字段,然后返回此自定义类
-
查看 Spring Data Rest 可以为您做什么docs.spring.io/spring-data/rest/docs/3.5.2/reference/html/…
-
我认为您可以使用 dto 实现这种格式,但您也可以查看分页。 JPA中有分页和排序存储库,它提供了很多信息。
标签: java spring-boot api rest