简体   繁体   中英

How to support netsted resources in ActiveWeb

Two models named "Post" and "Comment", and I want to build RESTful route like /post/123/comment/8 and CommentsController handles all RESTful actions.

I have tried

route("/posts/{post_id}/comments/*").to(CommentsController.class);

and

route("/posts/{post_id}/comments/*").to(CommentsController.class).get().post().put().delete(); 

And none works:(

What you need is not a RESTful set of routes from the point of AvctiveWeb framework: http://javalite.io/routing#restful-routing .

What you are trying to do is to define some custom routes. Not to say that they are not REST - based though.

Here is the example that works:

The route:

public class RouteConfig extends AbstractRouteConfig{
    public void init(AppContext appContext) {
        route("/posts/{post_id}/comments/{comment_id}").to(CommentsController.class).action("index").get();
    }
}

The controller:

public class CommentsController extends AppController {
    public void index(){
        respond("index, post:" + param("post_id") + ", comment: " + param("comment_id"));
    }
}

then hit this:

http://localhost:8080/posts/123/comments/456

and observe:

index, post:123, comment: 456

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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