如何使用多级资源编写java jersey REST API?
我想编写一个多级的 REST API,例如:
/country
/country/state/
/country/state/{Virginia}
/country/state/Virginia/city
/country/state/Virginia/city/Richmond
我有一个 java 类,它是带有 @Path("country") 的 Country 资源
如何创建 StateResource.java 和 CityResource.java 以便我的 Countryresource 可以使用StateResource 以我计划的方式使用它?关于如何在 Java 中构造此类东西的任何有用链接?
I want to code a REST API that is multileveled like:
/country
/country/state/
/country/state/{Virginia}
/country/state/Virginia/city
/country/state/Virginia/city/Richmond
I have a single java class that is a Country resource with @Path("country")
How do I create a StateResource.java and CityResource.java so that my Countryresource can use the StateResource in the way I am planning to use it? Any useful links on how to construct this kind of thing in Java?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
CountryResource
类需要有一个使用@Path
注释到子资源CityResource
的方法。默认情况下,您负责创建CityResource
实例,例如CityResource
提供处理 HTTP 动词的方法(在本例中为GET
)。您应该查看有关子项目的 Jersey 文档 - 资源定位器以获取更多信息。
另请注意,Jersey 提供了 ResourceContext 获取it来实例化子资源。如果您要在子资源中使用
@PathParam
或@QueryParam
我相信您需要使用它,因为运行时在创建时不会触及子资源您自己通过new
。The
CountryResource
class needs to have a method annotated with the@Path
to the sub-resourceCityResource
. By default, you are responsible for creating the instance ofCityResource
e.g.CityResource
provides the methods that handle the HTTP verbs (GET
in this case).You should look at the Jersey documentation regarding sub-resource locators for more info.
Also note that Jersey provides a ResourceContext to get it to instantiate the sub-resource. If you're going to use
@PathParam
or@QueryParam
in the sub-resource I believe you need to use this as the runtime doesn't touch sub-resources when created yourself vianew
.