Handler paths

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Handler paths

uris77
I'm trying to do something like this:

In Ratpack.groovy:

handler("SomeHandler", registry.get(SomeHandler))

And in SomeHandler.groovy

context.with{
  get(":id") {
     ....
  }
 
  post {
  }

 get {
 }

}

But I can't add a path in the Handler. What is the recommended way of doing this? I just don't like to have a special handler for retrieving by id, and one for retrieving a list.
rus
Reply | Threaded
Open this post in threaded view
|

Re: Handler paths

rus
I think you need to look at GroovyChainAction  Something like this

public class FooHandler extends GroovyChainAction {

    @Override
    protected void execute() throws Exception {

      handler(":id") {
        byMethod {
          get {
            render "get $pathTokens.id"
          }
        }
      }

      handler {
        byMethod {
          get {
            render "get"
          }
          post {
            render "post"
          }
        }
      }

    }
  }

and then use it like

handlers {
      prefix("foo") {
        handler chain(new FooHandler())
      }
}
Reply | Threaded
Open this post in threaded view
|

Re: Handler paths

uris77
Nice. Thank you.
Reply | Threaded
Open this post in threaded view
|

Re: Handler paths

pcbalodi
In reply to this post by rus
Hi, This method does not work for me, I am not sure but maybe the api has changed. After creating the FooHandler class, I get MissingMethodException for the handler.
Also I am wondering which is the correct approach, implementing the action chain, as here - http://blog.stumblingoncode.com/posts/2014-10-20-ratpack-groovy-chain.html
or extending the GroovyActionChain Classs. Sadly None of the two are working right now for me though.
rus
Reply | Threaded
Open this post in threaded view
|

Re: Handler paths

rus
Your best bet is to check out example-books.  It has an example of this and is always kept up-to-date with any api changes https://github.com/ratpack/example-books/blob/latest/src/ratpack/ratpack.groovy#L162