restroutesjavaliteactiveweb

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:(


Solution

  • 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