Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve nested routes API [SPR-14954] #19521

Closed
spring-projects-issues opened this issue Nov 25, 2016 · 3 comments
Closed

Improve nested routes API [SPR-14954] #19521

spring-projects-issues opened this issue Nov 25, 2016 · 3 comments
Assignees
Labels
in: web type: enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

@spring-projects-issues spring-projects-issues commented Nov 25, 2016

Sébastien Deleuze opened SPR-14954 and commented

Nested routes API could perhaps be improved. Current syntax is :

subroute(GET("/level1/**"), route(GET("/foo/**"), r -> ok().body(fromObject("foo")))
	.andRoute(GET("/bar/**"), r -> ok().body(fromObject("bar")))
	.andRoute(GET("/baz/**"), r -> ok().body(fromObject("baz"))

It could be nice to allow to specifying subroute(prefix("/level1") instead of subroute(GET("/level1/**") (routing can apply to any method + no need to specify /** for this use case).

Also the method name subroute does not seems to be accurate here, especially for specifying the root route. And it would be nice to be able to use a fluent API (RouterFunction instance method ?) to specify the sub routes, not just with static methods not easy to discover from an API POV.


Issue Links:

  • #19112 HTTP request pattern matching refinements ("depends on")
  • #19520 Provide shortcut methods for routing
  • #19631 Provide a Kotlin DSL for the functional Web API

1 votes, 3 watchers

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Feb 16, 2017

Arjen Poutsma commented

Added the pathPrefix predicate, see 1d589eb

Still working on renaming subroute to something nicer.

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Feb 18, 2017

Sébastien Deleuze commented

Arjen Poutsma Even with Andy's fix I can't make nested routes based on pathPrefix works.

To give you a more concrete example, I would expect to be able to change such Kotlin routes

accept(APPLICATION_JSON).route {
        GET("/api/user/", this@UserController::findAll)
        POST("/api/user/", this@UserController::create)
        GET("/api/user/{login}", this@UserController::findOne)
        GET("/api/staff/", this@UserController::findStaff)
        GET("/api/staff/{login}", this@UserController::findOneStaff)
        GET("/api/speaker/", this@UserController::findSpeakers)
        GET("/api/speaker/{login}", this@UserController::findOneSpeaker)
        GET("/api/sponsor/", this@UserController::findSponsors)
        GET("/api/sponsor/{login}", this@UserController::findOneSponsor)
        GET("/api/{event}/speaker/", this@UserController::findSpeakersByEvent)
}

To

accept(APPLICATION_JSON).route {
    pathPrefix("/api").route {
        GET("/user/", this@UserController::findAll)
        POST("/user/", this@UserController::create)
        GET("/user/{login}", this@UserController::findOne)
        GET("/staff/", this@UserController::findStaff)
        GET("/staff/{login}", this@UserController::findOneStaff)
        GET("/speaker/", this@UserController::findSpeakers)
        GET("/speaker/{login}", this@UserController::findOneSpeaker)
        GET("/sponsor/", this@UserController::findSponsors)
        GET("/sponsor/{login}", this@UserController::findOneSponsor)
        GET("/{event}/speaker/", this@UserController::findSpeakersByEvent)
    }
}

(RequestPredicate.route() is calling internally RouterFunctions.nest())

Also I think pathPrefix should support pathPrefix("/api") and pathPrefix("/api/") variants

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Feb 20, 2017

Arjen Poutsma commented

Resolving as complete. We might still find issues with nested routes, but those should be filed as new JIRAs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web type: enhancement
Projects
None yet
Development

No branches or pull requests

2 participants