简体   繁体   English

如何在不同资源中重用REST方法?

[英]How to reuse a REST methods in different resources?

Hi, 嗨,

I am building a REST-api using Jersey and Java. 我正在使用Jersey和Java构建REST-api。 I wonder if it is possible to reuse a method in many resources. 我想知道是否有可能在许多资源中重用一种方法。

As an example If I have this code: 例如,如果我有此代码:

@Path("/users")
public class UserResource {
    @GET
    @Path("/{uid}/comments")
    @Produces(MediaType.APPLICATION_JSON)
    public List<Comment> getComments() {
        return commentService.getOnEntity("User", uid);
    }   
}

and this: 和这个:

@Path("/items")
public class ItemResource {
    @GET
    @Path("/{uid}/comments")
    @Produces(MediaType.APPLICATION_JSON)
    public List<Comment> getComments() {
        return commentService.getOnEntity("Item", uid);
    }   
}

Is it possible to reuse the code for specifying the method "/{uid}/comments/" so I do not need to write it in every resource that is going to need it? 是否可以重用用于指定方法“ / {uid} / comments /”的代码,这样我就不必在将需要它的每个资源中编写它了?

I guess I could extend a CommentResource with the method, but the I can only add one set of methods. 我想我可以用该方法扩展CommentResource,但是我只能添加一组方法。 If I use Interface I could specify more than one set of methods but would have to rewrite the code inside the methods in every resource. 如果使用接口,则可以指定一组以上的方法,但是必须在每个资源中的方法内部重写代码。

Edit After a tips from @thomas.mc.work I rewrote my code using a sub resource. 编辑从@ thomas.mc.work获得提示后,我使用子资源重写了我的代码。 It is better than the first solution since I get all methods from my sub resource and it only takes 4 lines of code per resource. 它比第一个解决方案更好,因为我从子资源中获取了所有方法,并且每个资源仅占用4行代码。 This is how it looks like: 它是这样的:

@Path("/users")
public class UserResource {

    @Path("/{uid}/comments")
    public CommentSubResource getCommentSubResource(@PathParam("uid") String uid) {
        return new CommentSubResource("User", uid);
    }   
}

and this: 和这个:

@Path("/items")
public class ItemResource {

    @Path("/{uid}/comments")
    public CommentSubResource getCommentSubResource(@PathParam("uid") String uid) {
        return new CommentSubResource("Item", uid);
    }   
}

and this: 和这个:

public class CommentSubResource {

    private String entity;
    private String entityUid;

    public CommentSubResource(String entity, String entityUid) {
        this.entity = entity;
        this.entityUid = entityUid;
    }

    @GET
    @Path("/")
    @Produces(MediaType.APPLICATION_JSON)
    public List<Comment> getComments() {
        return commentService.getOnEntity(entity, entityUid);
    }

    @DELETE
    @Path("/")
    @Produces(MediaType.APPLICATION_JSON)
    public List<Comment> deleteComment(@PathParam("uid") String uid) {
        return commentService.delete(uid);
    }
}

This is much better. 这样好多了。 I have an idea to use java 8 and default implementation interfaces to be able to just implmenet an interface to get the functionality, but I am not sure if I am able to determine which resource the default implemented method is called from. 我有一个想法,可以使用Java 8和默认实现接口来实现接口的功能,但是我不确定是否可以确定从哪个资源调用默认实现的方法。

Edit 编辑
After some laboration I think subresources is the way to go, even if it´s not (according to me) the perfect solution. 经过一些努力之后,我认为子资源是必经之路,即使(根据我的观点)这不是完美的解决方案。

There is a similar feature called "Subresource Locators" . 有一个类似的功能称为“子资源定位符” You can decide in runtime which Resource is selected to process the request that is matching your JAX-RS method. 您可以在运行时决定选择哪个资源来处理与您的JAX-RS方法匹配的请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM