How to list all routes in Java [Play 2.5]

705 views
Skip to first unread message

Rodrigo Sol

unread,
Jun 30, 2016, 3:07:33 PM6/30/16
to play-framework
Hi Guys,

I need to list all routes from a unit testing using Java. I found some scala examples, but I could not implement them in java.
Can someone provides me some code snippet?

Thanks


Greg Methvin

unread,
Jun 30, 2016, 3:34:23 PM6/30/16
to play-framework
Hi Rodrigo,

You can inject your Router (the Java play.routing.Router) and call router.documentation(). That gives you a list of RouteDocumentation objects that include the method, path pattern, and a string representing the controller invocation.

I think we are actually missing documentation for the Java Router object. That was a new Java API we added in 2.5.

Greg

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framewor...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/a11038de-3cc5-477f-a4ac-55701c45db7b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Greg Methvin
Senior Software Engineer

Rodrigo Sol

unread,
Jun 30, 2016, 3:39:46 PM6/30/16
to play-framework
Hi Greg,

Actually I just made this work using: 

List<Router.RouteDocumentation> myRoutes = app.getWrappedApplication().routes().asJava().documentation();

Should I choose this new API over the code above, or it does not matter?

Thank you

Greg Methvin

unread,
Jun 30, 2016, 3:45:57 PM6/30/16
to play-framework
The routes method of Application is deprecated, so it would be best to avoid that. In a test, it should be acceptable to get the instance from the injector directly:

app.injector().instanceOf(play.routing.Router.class).documentation()


For more options, visit https://groups.google.com/d/optout.
Message has been deleted

Brainztorm

unread,
Dec 20, 2016, 9:56:29 AM12/20/16
to Play Framework
Hi,

I made this work with :
public class Application extends Controller {

@Inject
private Provider<Router> router;


public Result home() {
List<Router.RouteDocumentation> rds = router.get().documentation();

...

Thanks to Greg advice here : https://groups.google.com/forum/#!searchin/play-framework/routes$20list|sort:relevance/play-framework/vEYA2m_vxCU/2Adn3-YgBAAJ

Play framework is becoming so complexe ! Is there someone here that can explain why we need a "Provider<Router>" and not a simple "Router" ?

I find it very difficult to find documentation about DI in play and where i can find other Play entities like Router i can use ... any ressources/videos/books ?

Thanks !!

François

Christian Schmitt

unread,
Dec 20, 2016, 2:33:48 PM12/20/16
to Play Framework
It's not complex, but you are doing something that *can* be quite complex.

consider class a and class b if class a depends on class b it's nearly impossible that class b also depends on class a, since that would bea cycle dependency.
Even if you write that in java code you would need a object in between that handle's that for you. Since a can't be created without b and vice versa.

A provider injection can handle that case for you.
https://github.com/google/guice/wiki/CyclicDependencies

Greg Methvin

unread,
Dec 21, 2016, 12:29:16 AM12/21/16
to play-framework
It's actually much simpler to reason about than it used to be, especially when you're dealing with third-party code that depends on various Play components.

--
You received this message because you are subscribed to the Google Groups "Play Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framework+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/9d92d58f-c6cc-4d41-9747-6586a4c08bea%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Greg Methvin
Tech Lead - Play Framework

Message has been deleted

Brainztorm

unread,
Dec 21, 2016, 3:30:26 AM12/21/16
to Play Framework
Hi, thanks to both of you !

It's much clearer now.
Another question ... where i can find the others Play entities/components/libs that we can use ? ( i now can inject the Router but what other things can i inject too :) is there a list somewhere ?)

Have a great day !
Reply all
Reply to author
Forward
0 new messages