HandlerDecorator registration via guice 0.9.15 vs 0.9.17

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

HandlerDecorator registration via guice 0.9.15 vs 0.9.17

CharlieK
Hi all,
I am trying to update a toy application from Ratpack 0.9.15 to ratpack 0.9.17 as an example and am running into an issue caused by HandlerDecorator no longer automatically automatically registering routes within it. I was wondering the best way to do this in the newer versions of ratpack.

For example in my toy java applicaiton I have a module that looks like this:

```java
public class ApiHandlerDecorator implements HandlerDecorator {

  @Override
  public Handler decorate(Registry serverRegistry, Handler rest) throws Exception {
    return Handlers.chain(rest, Handlers.chain(serverRegistry, c -> {
      c.get("api/timed", TimedHandler.class);
      // ... many other routes registered here
    }));
  }
}
```

In 0.9.15 once I do this I can go to /api/timed and see the output of the TimedHandler, however in 0.9.17 no error is shown but the routes are not registered.  My goal of doing things this way is to allow many different modules to add routes and make the application plugable, and avoid needing every method to be listed in the main method builder.

The full working 0.9.15 toy application can be seen here:
https://github.com/charliek/hopscotch/tree/update_question

I have a PR out where I tried to update and am not seeing the routes being registered anymore here:
https://github.com/charliek/hopscotch/pull/1

I've also added a couple comments into the PR that might help explain the issue further.

My hope is there is still a concise way to do this from within a module so that this type of thing can be used for a kind of a "plugin" type system where extra functionality can be added by modules.

Thanks for your help,

Charlie
Reply | Threaded
Open this post in threaded view
|

Re: HandlerDecorator registration via guice 0.9.15 vs 0.9.17

johnrengelman
Hey Charlie,

In your "HopscotchApp" class when you configure the Guice bindings, you want to do a `b.module(new HopscotchModule())` instead of `b.add(new HopsotchModule())`.
Using the `module(Module)` method tells the framework to configure the module and all the module itself to install/add bindings to the registry.

I made this change locally on your PR and verified that it works. I'm trying to find documentation around this behavior/change and will post here when I find some.
Reply | Threaded
Open this post in threaded view
|

Re: HandlerDecorator registration via guice 0.9.15 vs 0.9.17

johnrengelman
The relevant change was here: https://github.com/ratpack/ratpack/commit/a2e46292bbd30466b16cfba506a7cb7a521eab9d

In 0.9.17, we changed the internal structure of the Guice BindingsSpec to extend from the built-in Ratpack RegistrySpec. The 'add' method conflicted between these two classes, so the underlying RegistrySpec.add method won since it was lower level and the BindingsSpec.add method was renamed to BindingsSpec.module.

What was happening in your app, is that you were effectively adding your module instance to the Ratpack Registry instead of the Guice bindings.
Reply | Threaded
Open this post in threaded view
|

Re: HandlerDecorator registration via guice 0.9.15 vs 0.9.17

danhyun
In addtion to John's note I have submitted a PR https://github.com/charliek/hopscotch/pull/2 with a slightly stylized approach. Note with HandlerDecorators we generally want them all to run so using Multibinder is helps achieve this. We also use Service to initialize RxRatpack integration.
Reply | Threaded
Open this post in threaded view
|

Re: HandlerDecorator registration via guice 0.9.15 vs 0.9.17

CharlieK
In reply to this post by johnrengelman
Thanks John. That makes sense and worked great.
Reply | Threaded
Open this post in threaded view
|

Re: HandlerDecorator registration via guice 0.9.15 vs 0.9.17

CharlieK
In reply to this post by danhyun
Thanks Danny. That is cleaner and forced me to read over the MultiBinder docs again which are not as bad as I remember. Now to make this app do something a bit more useful.