【问题标题】:Naming convention of API endpointsAPI 端点的命名约定
【发布时间】:2021-05-25 12:28:14
【问题描述】:

我有这个实体。

@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;
}

我想在控制器中创建删除映射

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

控制器的终点应该是什么? 或者我写的方式是正确的方式?

【问题讨论】:

    标签: spring api java-8 endpoint spring-restcontroller


    【解决方案1】:

    由于您的端点操作(HTTP 操作)属于 DELETE 类型,因此您已经在端点上声明了您的意图。所以你写端点的方式在我看来(和一般惯例)是正确的。

    但是,如果您已经制定了约定,请使用您的项目中已经使用的任何约定。一致性是关键。

    如果它是一个干净的石板,不要在端点中添加 CRUD 函数名称。

    如果您想要更深入的答案,我会通读 this 资源。但主要的收获是在项目中保持一致。因此,如果您选择该约定,请坚持下去。

    【讨论】:

      猜你喜欢
      • 2021-07-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-23
      • 2020-11-14
      • 1970-01-01
      • 2021-12-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多