我有个问题。当我有其他实体时,我不知道如何创建 API。我与 Postman 合作,当我请求查看数据库中的所有项目时,我也想接收实体。
例如,这是我的实体:
@Entity
@Table(name = "project")
public class Project {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "proj_id")
private int projectId;
@Column(name = "project_name")
private String projectName;
@Column(name = "dg_number")
private int dgNumber;
@ManyToMany
@JoinTable(name = "project_gate_relation", joinColumns = @JoinColumn(name = "proj_id"), inverseJoinColumns = @JoinColumn(name = "gate_id"))
@JsonBackReference
private List<Gate> gates;
@ManyToMany
@JoinTable(name = "project_threshold_relation", joinColumns = @JoinColumn(name = "proj_id"), inverseJoinColumns = @JoinColumn(name = "thresholdgates_id"))
@JsonBackReference
private List<Threshold> thresholds;
这是门实体
@Entity
@Table(name = "gate")
public class Gate {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "gate_id")
private int gateId;
@Column(name = "gate_type")
private String gateType;
@Column(name = "gate_value")
private float value;
阈值实体
@Entity
@Table(name = "threshold")
public class Threshold {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "threshold_id")
private int thresholdId;
@Column(name = "threshold_value")
private int thresholdValue;
@Column(name = "threshold_type")
private String thresholdType;
控制器
@RestController
@RequestMapping(ProjectController.PROJECT_URL)
public class ProjectController {
public static final String PROJECT_URL = "/cidashboard/projects";
@Autowired
private final ProjectService projectService;
public ProjectController(ProjectService projectService) {
this.projectService = projectService;
}
@GetMapping
public List<Project> getAllProjects(){
return projectService.findAllProjects();
}
@GetMapping("/{id}")
public Project getProjectById(@PathVariable int id) {
return projectService.findProjectById(id);
}
阿波罗的战车
犯罪嫌疑人X
相关分类