如何使用多级资源编写java jersey REST API?

发布于 2024-12-18 19:11:34 字数 357 浏览 0 评论 0原文

我想编写一个多级的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

感受沵的脚步 2024-12-25 19:11:34

CountryResource 类需要有一个使用 @Path 注释到子资源 CityResource 的方法。默认情况下,您负责创建 CityResource 实例,例如

@Path("country/state/{stateName}")
class CountryResouce {

    @PathParam("stateName")
    private String stateName;

    @Path("city/{cityName}")
    public CityResource city(@PathParam("cityName") String cityName) {
        State state = getStateByName(stateName);

        City city = state.getCityByName(cityName);

        return new CityResource(city);
    }

}

class CityResource {

    private City city;

    public CityResource(City city) {
        this.city = city;
    }

    @GET
    public Response get() {
        // Replace with whatever you would normally do to represent this resource
        // using the City object as needed
        return Response.ok().build();
    }
}

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-resource CityResource. By default, you are responsible for creating the instance of CityResource e.g.

@Path("country/state/{stateName}")
class CountryResouce {

    @PathParam("stateName")
    private String stateName;

    @Path("city/{cityName}")
    public CityResource city(@PathParam("cityName") String cityName) {
        State state = getStateByName(stateName);

        City city = state.getCityByName(cityName);

        return new CityResource(city);
    }

}

class CityResource {

    private City city;

    public CityResource(City city) {
        this.city = city;
    }

    @GET
    public Response get() {
        // Replace with whatever you would normally do to represent this resource
        // using the City object as needed
        return Response.ok().build();
    }
}

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 via new.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文