首页 > 解决方案 > 如何使用控制器制作像 /{Country}/{city in this country}/ 这样的链接

问题描述

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

标签: javaspringcontrollerspring-data-jpamapping

解决方案


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

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

推荐阅读