RESTful端点和数据库关系

时间:2018-04-03 18:57:43

标签: rest restful-architecture restful-url

我是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 个答案:

答案 0 :(得分:1)

/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

答案 1 :(得分:1)

我想(在查看数据库架构之后),您的所有<head> <meta charset="utf-8"> <meta content="IE=edge" http-equiv="X-UA-Compatible"> <meta content="width=device-width, initial-scale=1" name="viewport"> <title>Owl Slider</title> <link href="scripts/owl.carousel.css" rel="stylesheet"> <link href="scripts/owl.theme.default.css" rel="stylesheet"> <link href="scripts/stuff-style-carousel-3.css" rel="stylesheet"> <script src="https://code.jquery.com/jquery-1.12.4.min.js"> </script> <script src="scripts/owl.carousel.min.js"> </script> </head> <body> <h1>Video with&nbsp;100% width of the browser window and maintains its aspect ratio</h1> <div class="owl-carousel owl-theme owl-loaded owl-drag" id="owl4"> <div class="owl-stage-outer"> <div class="owl-stage" style="transform: translate3d(0px, 0px, 0px); transition: 0s; width: 1196px;"> <div class="owl-item active owl-video-playing" data-video="https://www.youtube.com/watch?v=1Dhn6yCygS8" style="width: 598px;"> <div class="owl-item-container video-aspect-16-9" data-aspect="1.7777778"> <div class="owl-video-wrapper"> <a class="owl-video" href="https://www.youtube.com/watch?v=1Dhn6yCygS8" style="display: none;"></a> <div class="owl-video-frame"> <iframe allowfullscreen frameborder="0" height="251" src="//www.youtube.com/embed/1Dhn6yCygS8?autoplay=1&amp;rel=0&amp;v=1Dhn6yCygS8" width="100%"></iframe> </div> <div class="owl-video-play-icon"></div> <div class="owl-video-tn" style="opacity:1;background-image:url(//img.youtube.com/vi/1Dhn6yCygS8/hqdefault.jpg)"></div> </div> <div class="owl-text-container"> <h2>This video is 19:9</h2><a class="owl-image-link" href="#"> <p>Some text.</p></a> </div> </div> </div> <div class="owl-item" data-video="https://www.youtube.com/watch?v=S03UOkb9zU4" style="width: 598px;"> <div class="owl-item-container video-aspect-4-3" data-aspect="1.333333"> <div class="owl-video-wrapper"> <a class="owl-video" href="https://www.youtube.com/watch?v=S03UOkb9zU4" style="display: none;"></a> <div class="owl-video-play-icon"></div> <div class="owl-video-tn" style="opacity:1;background-image:url(//img.youtube.com/vi/S03UOkb9zU4/hqdefault.jpg)"></div> </div> <div class="owl-text-container"> <a class="owl-image-link" href="#"> <h2>This video is 4/3.</h2> <p>Some text.</p></a> </div> </div> </div> </div> </div> <div class="owl-nav disabled"> <button class="owl-prev" role="presentation" type="button"><span aria-label="Previous">‹</span></button><button class="owl-next" role="presentation" type="button"><span aria-label="Next">›</span></button> </div> <div class="owl-dots"> <button class="owl-dot active" role="button"><span></span></button><button class="owl-dot" role="button"><span></span></button> </div> </div> <script> $(document).ready(function(){ $('#owl4').owlCarousel({ items:1, margin:0, dots:true, video:true, onPlayVideo: videoSize }); function videoSize(event){ var video = $(document).find(".owl-video-frame"); console.log(video); } }); </script> </body> statesregions已经拥有唯一标识符,您可以简单地修改{{1来自provinces以外的所有资源。这与使用查询参数过滤/api/states / states特定ID的事实相结合。

最终会得到以下端点

provinces

现在有了这些路由/端点,如何通过API实际创建这些资源也很明显。如果API的客户端知道资源的regions结果,请使用+----------------------------------------------+---------------------------------------------------------+ | 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 | +----------------------------------------------+---------------------------------------------------------+ POST

另请考虑@ roman-vottner在上述评论中所说的内容。

答案 2 :(得分:0)

我完全和你在一起:

/ api / states - 是的,列出所有州 / api / states / {id} - 是的,显示一个州
/ api / states / {id} / regions - 是的,只是那个州的区域

但现在我会走这条路:

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

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

答案 3 :(得分:0)

相反,您可以拥有一个处理所有查询组合的Controller。 例如,您可以将Controller 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);

    }