REST api service context and resources url

时间:2016-04-15 11:08:33

标签: java rest jersey jax-rs microservices

We have serveral services running on an application server and every service has a context. The name of the service is automatically added to the url, since there can be multiple services on the same application server.
Now we are creating a new service, which is called Draws, meaning the url will be

http://url:port/Draws

However, now the discussion is the api paths (Resources) to this service. Since we are getting draws, in my mind this should be draws. Which means it will have the url

http://url:port/Draws/draws/{gameNo}

2x draws - Thoughts?

There are thoughts here that the service does only have draws and therefor Draws/{gameNo} is enough.
But in my mind, draws resource is the api interface of the service, like Draws is the book in a library, draws is the chapter... And it should be possible to add more chapters to the book.

Then to implementation, we are using Jersey. That would mean we would have a resource with @Path("{gameNo}").

Edit 1:
There are gateways in front of our services, so the context will never be exposed to end users, it's only there to point to an specific service. Since multiple services can run on the same host:port

Edit 2:
Context part of the url is part of the service discovery lookup

Edit 3:
We are actually not versioning in url, but in Accept header, so actually my url is the same as clementinos but the version part of the url

3 个答案:

答案 0 :(得分:1)

我会避免使用2x'draw'。

这是设计URI结构的可能方法。 请注意,这些段应该是小写的(所以不要使用'Draws')

<scheme>://<host>[:<port>]/<api-path>/<api-name>/<api-version>/<resource-path>
  • 计划(例如http)
  • 主机是一个完全限定的主机名,是隐藏API实施的设备和物理位置的DNS别名。它包含有关非生产环境(test,int)时的环境信息。
  • 端口应该是默认的http端口(80),因此可以省略。其他端口可用于非生产环境。
  • api 路径将REST API与服务器提供的其他资源(例如webapp)分开。它通常是/ api的形式。对于仅提供REST apis的服务器,可以省略它。
  • api name 在一种包中收集一组资源。这是发布和版本控制的单位。
  • api版本是API的版本。它的格式为v [major-version-number]
  • 资源 路径由资源URI段组成

答案 1 :(得分:0)

不要在书本,图书馆或章节中弄乱你的思想。只看你的实体:“吸引”你的实体吗?那么它应该是http://url:port/draws/{gameNo}

对于Rest API设计,您可以阅读以下资源:

我建议你看看Richardson成熟度模型。

我也喜欢Github API。 (过去他们曾经建议我们阅读代码来提高我们的才能,现在你也可以阅读其他的API。)

答案 2 :(得分:0)

如果您设计了Draws服务,那么您应该是:

  

http://url:port/draws/ {gameNo}

如果您计划使用其他端点扩展服务,可能您正在设计更通用的东西。如果是这种情况,请相应地命名。

无论如何,你不应该使用抽奖/抽奖。

另外,我建议使用小写和https(如果可以的话)。

最近,我发布了设计RESTful API时出现的最常见问题的摘要 - How To Design Practical RESTful API,这可能会有所帮助。

相关问题