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

View Controller In Spring WebFlux [SPR-17031] #21569

Closed
spring-projects-issues opened this issue Jul 11, 2018 · 5 comments
Closed

View Controller In Spring WebFlux [SPR-17031] #21569

spring-projects-issues opened this issue Jul 11, 2018 · 5 comments
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: declined A suggestion or change that we don't feel we should currently apply type: enhancement A general enhancement

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Jul 11, 2018

Ankur Pathak opened SPR-17031 and commented

Their is no support for View Controller In Spring Web Flux.
Why not have addViewControllers(ViewControllerRegistry registry) method in WebFluxConfigurer similar to WebMvcConfigurer?

Their is no support for Welcome page like index.html also.


Affects: 5.0 GA

Reference URL: spring-projects/spring-boot#9785

@spring-projects-issues
Copy link
Collaborator Author

Rossen Stoyanchev commented

Indeed no reason not to. I'm assigning to 5.x backlog only because it's a bit late for the 5.1 backlog.

@spring-projects-issues
Copy link
Collaborator Author

Rossen Stoyanchev commented

For the welcome page, we can make sure a default page can be mapped with a view controller but the actual welcome page would have to come from Boot.

@spring-projects-issues
Copy link
Collaborator Author

Ankur Pathak commented

Can't we have Servlets semantics for welcome page. We would register default.html, index.html, index.htm as default views in view registry and render them in order. In case we use explicit view mapped to root path, it will overwrite them.

@spring-projects-issues spring-projects-issues added type: enhancement A general enhancement in: web Issues in web modules (web, webmvc, webflux, websocket) labels Jan 11, 2019
@spring-projects-issues spring-projects-issues added this to the 5.x Backlog milestone Jan 11, 2019
@rstoyanchev rstoyanchev changed the title Support For View Controller In Spring WebFlux [SPR-17031] View Controller In Spring WebFlux [SPR-17031] Jan 29, 2019
@bclozel bclozel self-assigned this Apr 7, 2020
@bclozel
Copy link
Member

bclozel commented May 19, 2020

Taking a step back, the main goals behind this are in my opinion:

  • feature parity with Spring MVC
  • providing a way to register views without writing actual controller handlers
  • allowing Spring Boot to register the "welcome page" (not related to Servlet welcome pages!)

In Spring MVC, this is achieved thanks to the Controller and AbstractController hierarchy: we've got specific implementations that map paths to ModelAndView values pointing to views. We're also supporting more features, like customizing the HTTP response status.

In Spring WebFlux, the infrastructure is different and we can't implement it the same way.
We could however use the RouterFunction to programmatically register such mappings and reuse the existing infrastructure (view resolvers, handler mapping, etc).

The implementation (minus the registry/registration classes) would look like this:

/**
 * Return the {@code RouterFunction} that contains the registered view
 * controller mappings, or {@code null} for no registrations.
 * @since 5.3.0
 */
@Nullable
protected RouterFunction<ServerResponse> buildRouterFunction() {

  return RouterFunctions.route()
      .GET("/", (req) -> ServerResponse.ok().render("home", Collections.emptyMap()))
      //...
      .build();
}

The implementation itself is really short and could provide more flexibility about ordering, response headers, or even path matching - like mapping /pages/{name} to the view name. In my opinion, path matching and ordering are powerful options for JavaScript client applications or applications with many static pages.

I've had a look at other web frameworks, like Symfony, Rails and others. It looks like the approach taken by Laravel is quite similar to using RouterFunction directly.

With that in mind, I've got questions for @rstoyanchev and @poutsma :

  1. Should we target feature parity with MVC in this case?
  2. Should we recommend instead RouterFunction directly?
  3. Is this approach (contributing a RouterFunction bean to the context) problematic in any way (ordering, filtering of RouterFunction, etc)? In MVC, we were contributing a HandlerMapping with a customizable order defaulting to 1.

@bclozel bclozel added the status: declined A suggestion or change that we don't feel we should currently apply label May 20, 2020
@bclozel bclozel removed this from the 5.x Backlog milestone May 20, 2020
@bclozel
Copy link
Member

bclozel commented May 20, 2020

After discussing the previous comment with team members, it seems that the main driver for this feature is the welcome page support in Spring Boot.

There are many ways to achieve the equivalent of ViewController in WebFlux, including:

  • a new DSL that creates a component based on a new Controller interface (taking a WebExchange and returning Object)
  • a registry that creates a RouterFunction directly

Because there is a way to directly implement the welcome page use case in Spring Boot, we'll close this issue for now until we get new requirements and use cases from the community.

@bclozel bclozel closed this as completed May 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: declined A suggestion or change that we don't feel we should currently apply type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants