Real World Controller Example with Spring 5: Web Reactive

I want to participate in the world of reactive programming with Spring. As I understand it, it gives me a choice between two different paradigms of: (a known to us @Controller, @RequestMapping) and jet ( which is designed to address the "Annotations hell" ).

My problem is that you do not understand what a typical reactive controller will look like. There are three conceptual interfaces that I can use in the controller class:

HandlerFunction<T>(1) - I define a method for each specific one ServerRequest  that returns a specific instance HandlerFunction<T>, then it registers these methods using a router. Correctly?

RouterFunction(2) and FilterFunction(3) - Is there a specific place where everything should be located RequestPredicatewith the appropriate HandlerFunction? Or can I do this separately in each controller (how was this done with the annotation approach)? If so, then how do you notify the global handler (router, if any?) To apply this part of the router from this controller?

Now I see the reactive template controller:

public class Controller {
    // handlers
    private HandlerFunction<ServerResponse> handleA() {
        return request -> ok().body(fromObject("a"));
    }

    // router
    public RouterFunction<?> getRouter() {
        return route(GET("/a"), handleA()).and(
               route(GET("/b"), handleB()));
    }

    // filter
    public RouterFunction<?> getFilter() {
        return route(GET("/c"), handleC()).filter((request, next) -> next.handle(request));
    }
}

And finally, how can I say that this is a controller without annotating it?

I read the Spring link and all posts related to this issue on the official blog. There are many patterns, but they are all taken out of context (IMHO), and I cannot put them together in a complete picture.

I would appreciate it if you could provide an example of the real world and good practices for organizing the interaction between these functions.

+4
2

:

RouterFunction - @Controller (@RequestMapping ) Spring :

(.. > ). , ; . RouterFunction @RequestMapping . : , , , ; : .

Spring Boot SpringApplication.run :

// route is your route function
HttpHandler httpHandler = RouterFunctions.toHttpHandler(route); 
HttpServlet servlet = new ServletHttpHandlerAdapter(httpHandler);
Tomcat server = new Tomcat();
Context rootContext = server.addContext("",
System.getProperty("java.io.tmpdir"));
Tomcat.addServlet(rootContext, "servlet", servlet);
rootContext.addServletMapping("/", "servlet");
tomcatServer.start();

, . Spring github

+2

Source: https://habr.com/ru/post/1665814/


All Articles