ninjaframework/ninja — pull request #195 by gitblit (2014-07-21 15:01:08+00:00)
Title: Optional annotation-based route definition and reverse routing improvements

While I think the Ninja framework has great potential, I am not a fan of the fragility of the routing mechanism. I agree with the concept of the single-file Routes registration approach, but in practice the dependence of a *named* method is a major weakness. Perhaps this can be improved with Java 8, but not likely all aspects of routing will be improved by that change.

This pull request gives developers the choice of how to register their routes:

1. single-file route definitions using discrete HTTP method / named controller method registration (`GET()...with(UsersController.class, "index")`)
2. annotation-based route definitions within the controller (`register(UsersController.class)`)

```java
public class UsersController {

@RouteDef(uri="/users")
public Result index() {
...
}

@RouteDef(uri="/users/{username}", order=2)
public Result getUser(@PathParam("username") String username) {
...
}

@RouteDef(uri="/users/{username}", method=HttpMethod.PUT)
public Result updateUser(@PathParam("username") String username) {
...
}
}
```
Additionally, this pull request implements *best-fit* reverse routing methods for the requested HTTP method where **best-fit** is defined as the matching route that has the fewest query parameters. This allows the most common use cases for reverse route generation to work without specifying the controller method name.

```java
// returns /users
String indexRoute = router.getReverseGET(UsersController.class);
// returns /users/james
String userRoute = router.getReverseGET(UsersController.class, "username", "james");
```

This pull request also fixes a potential null pointer exception which could occur during reverse route generation for a route that failed registration.

I can split this PR into multiple PRs, if that is your preference.