Override NinjaImpl invoke

74 views
Skip to first unread message

Darren Hudson

unread,
Jun 10, 2014, 9:28:58 AM6/10/14
to ninja-f...@googlegroups.com
This there any way to override or replace NinjaImpl?

The reason for this is that I am building an API layer, and the 404 and 400 responses are in html format, and I would like to be in the format of the inbound request, I.E JSON or XML.

The other reason for doing this is because I would like to build a annotation of @Async on route methods so that the request are bound to a worker thread pool instead on the inbound thread pool, and this will also enable annotations like @UnitOfWork to work as well.


Raphael André Bauer

unread,
Jun 11, 2014, 2:51:48 PM6/11/14
to ninja-f...@googlegroups.com
Hi Darren,


I think there are at least 2 solutions.
1st solution is to create a class called NinjaImpl, implement Ninja
and put it into package ninja. This overwrites the original class via
classpath shadowing. This allows you to start right away and try your
ideas without thinking too much about the while Ninja stack. But
that's a hack.

(There are even more ways how to handle that. Ninja is only a binding
to NinjaImpl, so you might want use stuff like Guice overrides or use
your own implicit binding in your module.)


2nd solution is actually what we want. The clean solution is to create
a class that allows to handle such situations - including the option
to implement your own router. I think Play2 has a good blueprint with
its Global class.

I'd imagine something like that:
interface Global {
public void onError(Result result, Context context);
public void onRouteNotFound(Result result, Context context);
public void onRouteRequest(Result result, Context context);
}

+ and implementation GlobalImpl which is then used in NinjaImpl.

This allows you to cleanly handle errors (with correct response
content type, custom responses and more) and situations where no route
is found. And it allows you to implement your own routing mechanism
(or even replace Ninja's router).

Your issue is similar to https://github.com/ninjaframework/ninja/issues/171 .


I might have some time end of June to look into the issue. But all
comments and PRs are really welcome :) It's something we need. Let me
know if you want to contribute :)


Cheers,


Raphael
> --
> You received this message because you are subscribed to the Google Groups
> "ninja-framework" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ninja-framewo...@googlegroups.com.
> To post to this group, send email to ninja-f...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ninja-framework/ddb819de-9293-4f7c-8723-8e82e3124730%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
inc: http://ars-machina.raphaelbauer.com
tech: http://ars-codia.raphaelbauer.com
web: http://raphaelbauer.com

Darren Hudson

unread,
Jun 13, 2014, 5:47:06 AM6/13/14
to ninja-f...@googlegroups.com
Hi Raphael,

Let me model something and share it with you.

Cheers,

Ross Oreto

unread,
May 3, 2021, 2:55:24 PM5/3/21
to ninja-framework
Since this solution was proposed 7 years ago, I want to find the current preferred method to implement a custom router?
So far I have  subclassed RouterImpl and in Module.java tried to: bind(Router.class).to(CustomRouter.class);
However this failed b/c the RouterImpl is already bound.

David

unread,
Nov 27, 2021, 6:06:59 AM11/27/21
to ninja-framework
I'm facing the exact same issue as ross.. any solution?

David

unread,
Nov 27, 2021, 6:15:02 AM11/27/21
to ninja-framework
Nevermind, found a solution:
1) Create new class inheriting from NinjaJetty
2) Create class CustomNinjaBaseModule with the same source as NinjaBaseModule
2) Override doPreConfigure()
3) Call overrideModule(new CustomNinjaBaseModule(ninjaProperties))

In your custom ninja base module you can now bind whatever router you want

Reply all
Reply to author
Forward
0 new messages