如何按照REST规则创建API端点



如果我按customerId 搜索项目,我希望输出以下格式

[
{id: "b793ca28-3732-408a-b661-d0542309f27d", projectName: "Project A"},
{id: "35203977-c739-4033-ade0-142cac031977", projectName: "Project B"}
]

我应该在端点中使用什么?如果我遵循REST规则,则为pathparamqueryparam

客户类别

@Entity
@Table(name = "customer")
public class Customer {

@Id
@GeneratedValue(generator = "UUID")
@GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
private String id;
private String customerName;
@OneToMany(mappedBy="customer", cascade = CascadeType.ALL)
private List<Project> projectList;
}

项目类别

@Entity
@Table(name = "project")
public class Project {
@Id
@GeneratedValue(generator = "UUID")
@GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
private String id;
private String projectName;
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name="customer_id", referencedColumnName = "id", nullable=false)
@OnDelete(action = OnDeleteAction.CASCADE)
private Customer customer;
}

我应该在端点中使用什么?pathparam或queryparam

Rest可能不应使用pathparamqueryparam
换句话说,您可以使用pathparamqueryparam

http://yourHost/customers/{customerId} <-- this is pathparam
http://yourHost/customers?customerId=3 <-- this is queryparam

但我建议需要参数pathparam,而

不需要参数因此,如果customerId是必需参数,我建议这样做。

http://yourHost/customers/{customerId}