代码之家  ›  专栏  ›  技术社区  ›  Diego

RESTful端点和数据库关系[关闭]

  •  0
  • Diego  · 技术社区  · 8 年前

    我在RESTful开发方面是新手。我正在开发一个小应用程序(使用SpringBoot),它显示州、地区和省份的列表。这是我的数据库图: enter image description here

    我在为资源创建端点时遇到问题。 创建这些端点是正确的:

    /api/states      **//List of all countries** 
    /api/states/{id} **//Show the single country**
    /api/states/{id}/regions  **//Show all regions**
    /api/states/{id}/regions/{id} **//Show the individual region**
    /api/states/{id}/regions/{id}/provinces **//Show all provinces**
    /api/states/{id}/regions/{id}/provinces/{id} **//Show the individual province**
    

    我意识到我的结束点太长了,我认为这不是正确的过程。 根据你的经验,你能推荐什么建议? 非常感谢。

    4 回复  |  直到 8 年前
        1
  •  1
  •   naina generalhenry    8 年前
    /api/states //List of all countries
    /api/states/{id} //Show the single country
    /api/states/{id}/regions //Show all regions
    

    以上端点良好。

    该问题存在于其余的嵌套中。假设区域和省ID是全局唯一的,则可以以相同的方式将其展平(不包括州)。

    以下是示例:

    /api/regions/{id} //Show the individual region
    /api/regions/{id}/provinces //Show all provinces
    /api/provinces/{id} //Show the individual province
    /api/provinces/{id}/towns //Show all towns
    
        2
  •  1
  •   flazzarini    8 年前

    我想(在查看了数据库模式之后)您的 states ,则, regions provinces 已经有了唯一的标识符,您只需对 /api/states 来自除 各州 。这与使用查询参数过滤的事实相结合 各省 / 区域 用于特定ID。

    因此将以以下端点结束

    +----------------------------------------------+---------------------------------------------------------+
    |                   Endpoint                   |                       Description                       |
    +----------------------------------------------+---------------------------------------------------------+
    | `/api/state`                                 | Get a list of States                                    |
    | `/api/state/<id>`                            | Get a specific State                                    |
    | `/api/region`                                | Get a list of regions                                   |
    | `/api/region?state_id=<id>`                  | Get a list of regions from a specific state             |
    | `/api/region/<id>`                           | Get a specific region                                   |
    | `/api/province`                              | Get a list of provinces                                 |
    | `/api/province?state_id=<id>&region_id=<id>` | Get a list of provinces based on state_id and region_id |
    | `/api/province/<id>`                         | Get a specific Province                                 |
    +----------------------------------------------+---------------------------------------------------------+
    

    现在,随着这些路由/端点的部署,如何通过API实际创建这些资源也有点显而易见。通过使用 POST PUT 如果API的客户端知道结果 <id> 资源的。

    此外,请考虑@roman vottner在其上述评论中所说的话。

        3
  •  0
  •   Task    8 年前

    在某种程度上,我完全支持你:

    /api/状态-是,列出所有状态
    /api/states/{id}-是,显示一个状态
    /api/状态/{id}/区域-是的,只有处于该状态的区域

    但现在我要走这条路:

    /api/regions/{id}-显示一个区域
    /api/地区/{id}/省份-显示该地区的所有省份
    /api/省/{id}-显示一个省

    我发现,每个请求只需要一个{id}。

        4
  •  0
  •   Karnabanu Ashalagari    8 年前

    相反,您可以使用一个控制器来处理所有查询组合。 例如,可以将控制器MapController映射到url/api/MapController。 在这个控制器中,我们可以定义一个方法,在这个方法中,我们可以使用逻辑来处理不同类型的查询,如下面的示例所示。

    @RestController
    @RequestMapping(value="/MapController")
    public class MapController {
    
        @RequestMapping(value="/getdata",method = RequestMethod.POST)
        public void process(@RequestBody Map<String, Object> inputs) 
        throws Exception {
           if(inputs.get("country_id")){
              //fetch specific country_data
              if(inputs.get("state_id")&&(state_id in country_data){
                   //fetch specific state_data
                   if(inputs.get("region_id")&&(region_id in state_data){
                          //fetch specific region_data
                          if(inputs.get("province_id")&&(province_id in region_data){
                               //fetch specific province_data
                          }
                          else{
                              return region_data.name //specific region details
                          }
    
                    }
                    else{
                       return state_data.name  //specific state details
                    }
               }
               else{
                    return country_data.name //specific country details
               }
           }
           else{
              //Return All the countries
           }
           System.out.println(payload);
    
        }