Jersey / JAX-RS中的嵌套资源 - 实现Restangular示例 [英] Nested resources in Jersey/JAX-RS -how to implement Restangular example

查看:137
本文介绍了Jersey / JAX-RS中的嵌套资源 - 实现Restangular示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,实际上可能不会从Rest视图中调用嵌套资源,但我对如何将Jersey类构造为静态提供程序感兴趣,因此它可以响应链接请求。

Thus may not actually be called nested resources from a Rest perspective, but I am interested in how to structure a Jersey class as a rest provider, so it can respond to chained requests.

即使我对基本/用户没问题,我可以使用/ users / 123来获取特定用户,但是如何分支到用户的属性.... / users / 123 / cars ,/ users / 123 / cars / 23等。

i.e I am ok with the basic /users, I am ok with /users/123 to get a specific user, but how to then branch down to properties of the user.... /users/123/cars, /users/123/cars/23 etc.

对于缺乏信息感到抱歉,但在Angular的Restangular文档中看到了这个例子。

Sorry for the lack of information, but saw this as an example in the Restangular documentation for Angular.

https://github.com/mgonto/restangular#production-应用程序使用

restangular

// Restangular returns promises
Restangular.all('users').getList()  // GET: /users
.then(function(users) {
  // returns a list of users
  $scope.user = users[0]; // first Restangular obj in list: { id: 123 }
})

// Later in the code...

// Restangular objects are self-aware and know how to make their own RESTful requests
$scope.user.getList('cars');  // GET: /users/123/cars

// You can also use your own custom methods on Restangular objects
$scope.user.sendMessage();  // POST: /users/123/sendMessage

// Chain methods together to easily build complex requests
$scope.user.one('messages', 123).one('from', 123).getList('unread');
// GET: /user/123/messages/123/from/123/unread


推荐答案

我认为资源定位器应该能够胜任。通常,他们会将请求重新定位到另一个能够使用它的资源。

I think resource locators should do the job. In general they're re-locating the request to a different resource, which is able to consume it.

在您的情况下,您将拥有一个根资源UserResource,将处理汽车的用户和子资源,消息 - CarsResource,MessagesResource。

In your case You'll have one root resource UserResource, which will handle users and sub-resources for cars, messages - CarsResource, MessagesResource.

根资源:

@Path("users")
class UsersResource {

    // GET: /users
    @GET
    @Path("{id}")
    public User getById(@PathParam("id") long id) {...}

    @Path("{id}/cars")
    public CarResource getCarResource(@PathParam("id") long userId) {
        return new CarResource(uesrId);
    }

    @Path("{id}/sendMessage")
    public MessagesResource getMessagesResourceForSend(@PathParam("id") long userId) {
        return new MessagesResource(userId);
    }

    @Path("{id}/messages")
    public MessagesResource getMessagesResourceForRead(@PathParam("id") long userId) {
        return new MessagesResource(userId);
    }
}

汽车和消息资源:

class CarsResource {
    long userId    

    // GET: /users/123/cars
    @GET
    public Car getAllCars() {
        /*retrieve all cars for user userId*/
    }

    // GET: /users/123/cars/3
    @GET
    @Path("{carId}")
    public Car getById(@PathParam("carId") carId) { 
        /*retrieve car for id carId*/
    }
}

class MessagesResource {
    long userId

    // POST: /users/123/sendMessage
    @POST        
    public void sendMessage(@FormParam("content") String content) {
        /*send message to user userId*/
    }

    // GET: /user/123/messages/123/from/123/unread
    @GET
    @Path("{id1}/from/{id2}/unread")
    public void getUnread(@PathParam("id1") long id1, @PathParam("id2") long id2) {
            /*return unread messages*/
    }
}

子资源不应注释在类级别上使用@Path,它们需要在Application类中注册JAX-RS runtinme

Sub-resources shouldn't be annotated with @Path on class level and they need to be registered with the JAX-RS runtinme in an Application class

这篇关于Jersey / JAX-RS中的嵌套资源 - 实现Restangular示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆