简体   繁体   English

API 端点的命名约定

[英]Naming convention of API endpoints

I have this entity.我有这个实体。

@Entity
public class Dealer{
    @EmbeddedId
    private DealerIdKey idKey;
   
    @NotNull
    
    private LocalDate date;
}
@Embeddable
@Data
public class DealerIdKey implements Serializable {

    private static final long serialVersionUID = 1L;
    @NotNull
    @Size(max = 6)
    private String code;
    @NotNull
    @Size(max = 4)
    private String des;
}

I want to create a delete mapping in the controller我想在 controller 中创建删除映射

@Autowired
private DealerRepository repo;
@DeleteMapping("/dealer/{id}")
    @ResponseBody
    public void delete(@NotNull @PathVariable(name = "id", required = true) DealerIdKey id) {
        repo.deleteById(id);
    }

What should be the end point of the controller? controller的终点应该是什么? Or the way I have written is the correct way?或者我写的方式是正确的方式?

As your endpoint action (HTTP action) is of type DELETE, you are already declaring your intent with your endpoint.由于您的端点操作(HTTP 操作)属于 DELETE 类型,因此您已经在端点上声明了您的意图。 So the way you wrote the endpoint is in my opinion (and general convention) correct.所以你写端点的方式在我看来(和一般惯例)是正确的。

But use whatever convention is already being used in your project, if you already have a convention going.但是,如果您已经制定了约定,请使用您的项目中已经使用的任何约定。 Consistency is key.一致性是关键。

If its a clean slate, don't add CRUD function names in an endpoint.如果它是一个干净的石板,请不要在端点中添加 CRUD function 名称。

I'd read through this resource if you want a more in-depth answer.如果您想要更深入的答案,我会通读资源。 But main takeaway is keeping consistent within a project.但主要的收获是在项目中保持一致。 So if you choose that convention, stick to it.因此,如果您选择该约定,请遵守该约定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM