gpt4 book ai didi

java - 具有 Spring RESTController 的端点的 REST API 层次结构

转载 作者:行者123 更新时间:2023-11-30 10:13:41 25 4
gpt4 key购买 nike

我有一些 REST API 的播放器资源 URI:

http://localhost:8080/player

http://localhost:8080/player/3 ----> id=3 的播放器资源的 URI

我有这个游戏资源的 URI:

http://localhost:8080/player/3/games

http://localhost:8080/player/3/games/5 ---> id = 3 的玩家(玩这个游戏的玩家)的 id = 5 的游戏资源的 URI。

使用 Spring 框架我想要两个 RestController,一个用于玩家资源,另一个用于游戏资源,但是使用 @RequestMapping 注释我有这个:

@RestController
@RequestMapping("${spring.data.rest.base-path}" + "/players")
public class PlayerRestResource {

@RequestMapping( method = RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED)
public PlayerDto createPlayer(@RequestBody PlayerDTO newPlayerDTO) {
...
}
....
}

但我不知道如何为这样的 gameRestResource 使用 RequestMapping 注释并获取玩家的 ID:

@RestController
@RequestMapping("${spring.data.rest.base-path}" + "/player/idplayer/games")
public class GameRestResource {

@RequestMapping( method = RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED)
public GameDto createGame(@RequestBody GameDTO newGameDTO) {
...
}
....
}

最佳答案

您必须在方法而不是类上添加您的特定映射。

为了保持一致性,您应该在路径中坚持使用单数或复数名词。例如。玩家对玩家或游戏对游戏。我更喜欢用单数名词来表示我的休息服务——但这主要是一种主观意见。请记住,您的路径应该只包含名词,绝不能包含动词(例如创建、检索、更新...等操作)。 GET、POST、PUT、DETELE 等 HTTP 方法是您的操作,因此您的路径中不需要动词。

您可以通过不同的方式归还资源。我建议你阅读这篇 question

@RestController
@RequestMapping("${spring.data.rest.base-path}" + "/player")
public class PlayerRestResource {

//This method can be accessed from /player/3
//Id need to be placed in curly. 3 from url will be passed to the method
@RequestMapping(path = "/{playerId}", method = RequestMethod.POST)
//Use @PathVariable to bind the value from to url to the method parameter.
public ResponseEntity<Player> getPlayer(@PathVariable("playerId") int playerId) {
}

//This is just like the above method.
//This method can be accessed from /player/3/game/5
@RequestMapping(path = "/{playerId}/game/{gameId}" method = RequestMethod.POST)
public ResponseEntity<List<Game>> getGame(@PathVariable("playerId) int playerId, @PathVariable("gameId) int gameId) {
}

}

格式化休息服务的快速速成类(class)。

您总是希望在您的路径之上进行构建。基本变量应该是您的基本实体。

创建新播放器 - 负载可以在正文中格式化为 JSON

POST:example.com/player

检索有关 ID 为 3 的玩家的信息。

获取:example.com/player/3

更新有关 ID 为 3 的播放器的信息 - 负载可以在正文中格式化为 JSON

PUT:example.com/player/3

删除 ID 为 3 的玩家

删除:example.com/player/3

检索与 ID 为 3 的玩家关联的 ID 为 5 的游戏的信息。请注意,此路径应用于为特定用户更新特定玩家的数据

获取:example.com/player/3/game/5

创建新游戏 - 负载可以在正文中格式化为 JSON

POST:example.com/game

检索有关 ID 为 5 的游戏的信息 - 此数据不与任何玩家关联。这只是关于 ID 为 5 的特定游戏的数据

获取:example.com/player/5

所有以/player 开头的路径都应该进入 PlayerController 类,所有以/game 开头的路径都应该在 GameController 类中。

我建议您阅读以下资源:

https://martinfowler.com/articles/richardsonMaturityModel.html

https://www.restapitutorial.com/

https://spring.io/guides/tutorials/bookmarks/

关于java - 具有 Spring RESTController 的端点的 REST API 层次结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51225524/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com