【问题标题】:How to with controller make link like /{Country}/{city in this country}/如何使用控制器制作像 /{Country}/{city in this country}/ 这样的链接
【发布时间】:2020-08-21 22:49:35
【问题描述】:

我在我的应用程序中使用了一个控制器。我使用了@RequestMapping,但我无法用它解决这个问题:假设 我的数据库中有一个包含国家列表的表,还有一个包含城市列表的表(一个国家与城市是一对多的)。我怎样才能确保当我点击一个国家时,我会转到这个国家的城市列表,链接就像 / 俄罗斯 / 莫斯科 p.s.抱歉英语不好

【问题讨论】:

    标签: java spring controller spring-data-jpa mapping


    【解决方案1】:

    您可以使用@PathVariable

    @GetMapping("{country}")
    public List<String> getCities(@PathVariable String counrty);
    

    按国家/地区获取城市列表,以及另一个

    @GetMapping("{country}/{city}")
    public String getCity(@PathVariable String counrty, @PathVariable String city);
    

    获取城市信息。

    【讨论】:

      【解决方案2】:

      遵循良好的端点构建实践应该是这样的:

      @GetMapping("/countries/{country}/cities")
      public ResponseEntity<?> listCitiesByIdCountry(@PathVariable long country) {
           //Code for get Cities for that idCountry
           return ResponseEntity.ok(list);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-06-19
        • 2014-09-04
        • 1970-01-01
        • 2016-11-23
        • 2012-11-17
        • 2020-12-31
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多