티스토리 뷰

개발

Spring Functional (WebFlux)

밥고양이 2021. 3. 28. 05:11

참고: www.youtube.com/watch?v=BFjrmj4p3_Y

 

docs.spring.io/spring-framework/docs/current/reference/html/web-reactive.html#webflux-new-framework

 

Web on Reactive Stack

The original web framework included in the Spring Framework, Spring Web MVC, was purpose-built for the Servlet API and Servlet containers. The reactive-stack web framework, Spring WebFlux, was added later in version 5.0. It is fully non-blocking, supports

docs.spring.io

 

package com.example.functionalspring;

import org.springframework.http.server.reactive.HttpHandler;
import org.springframework.http.server.reactive.ReactorHttpHandlerAdapter;
import org.springframework.web.reactive.function.server.HandlerFunction;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;

import reactor.core.publisher.Mono;
import reactor.netty.http.server.HttpServer;

public class FunctionalSpringApplication {
	public static void main(String[] args) throws Exception {

		HandlerFunction helloHandler = (ServerRequest req) -> {
			String name = req.pathVariable("name");
			return ServerResponse.ok().bodyValue("Hello" + name);
		};

		RouterFunction helloRouter = (ServerRequest req) -> {
			boolean isValidPath = RequestPredicates.path("/hello/{name}").test(req);
			return isValidPath ? Mono.just(helloHandler) : Mono.empty();
		};

		HttpHandler httpHandler = RouterFunctions.toHttpHandler(helloRouter);

		ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(httpHandler);

		HttpServer.create().host("localhost").port(8080).handle(adapter).bind().block();

		System.in.read();
	}

}

'개발' 카테고리의 다른 글

WebFlux 참고자료  (0) 2021.03.29
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함