java - What is the correct restful API design when we have optional behavior?
问题描述
I have use case where a class of students is broken down into teams. class has a classId and team has a teamId.
I have the following options:
Create 2 endpoints:
@GET @Path("/schoolMetadata/byClass/{classId}") @Produces(MediaType.APPLICATION_JSON) public Response getMetadataByClassId(
and
@GET
@Path("/schoolMetadata/byTeam/{teamId}")
@Produces(MediaType.APPLICATION_JSON)
public Response getMetadataByTeamId(
Second implementation would be using query param.
@GET @Path("/schoolMetadata/by/") @Produces(MediaType.APPLICATION_JSON) public Response getMetadataById( @QueryParam("classId") final Id classId @QueryParam("teamId") final Id groupId) { if (classid != null) { } else { } }
Which of the 2 approaches is better ?
解决方案
I guess the first approach is better because it fits the Single Responsibility Principle (SOLID).
The single responsibility principle (SRP) asserts that a class or module should do one thing only. Now, this is kind of subjective, so the principle is reinforced with the heuristic that the class or module should have only one reason to change.
1 approach has two methods (modules) and each do only one thing
推荐阅读
- python - Python Discord Bot 嵌入用户信息
- sql-server - Kedro 找不到 SQL Server 表
- java - 从 Angular 中的 MultiValueMap 获取值
- angular - 是否可以使用 ag-grid-community 从 ag-grid 复制行数据?
- flutter - 未处理的异常:“字符串”类型不是“列表”类型的子类型
- reactjs - 如何在material-ui datagrid中实现搜索?
- google-chrome - 防止 Chrome DevTools 在按 F10 时搅动源代码页
- python - gurobipy:使用 python API 编写分段线性约束(SOS2 约束)的最佳方式
- graphql - 如何使用 graphql 更新 shopify 上的变体标题
- php - Laravel Fortify 电子邮件验证重定向到 /home