Notice
Recent Posts
Recent Comments
Link
개발자는 기록이 답이다
패스트캠퍼스 환급챌린지 26일차 미션 (2월 26일) : Spring Webflux 완전 정복 : 코루틴부터 리액티브 MSA 프로젝트까지 강의 후기 본문
패스트캠퍼스
패스트캠퍼스 환급챌린지 26일차 미션 (2월 26일) : Spring Webflux 완전 정복 : 코루틴부터 리액티브 MSA 프로젝트까지 강의 후기
slow-walker 2024. 2. 26. 23:24webhandler
WebHandler는 Spring WebFlux에서 HTTP 요청을 처리하고 응답을 생성하는 역할을 수행합니다. Spring WebFlux는 함수형 엔드포인트와 함께 사용되는데, 이때 WebHandler는 다양한 요청에 대한 처리를 담당합니다.
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
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;
@Configuration
public class MyRouter {
@Bean
public RouterFunction<ServerResponse> route() {
return RouterFunctions.route()
.GET("/hello", this::handleHello)
.build();
}
private Mono<ServerResponse> handleHello(ServerRequest request) {
return ServerResponse.ok()
.contentType(MediaType.TEXT_PLAIN)
.bodyValue("Hello, WebFlux!");
}
}
webhandler는 httphandler의 단점을 극복한 컴포넌트이다.
- Filter, Codec, ExceptionHandler 등을 지원
- ServerWebExchange를 제공
- Mono<vVoid>를 반환하는 메소드 가 있다
ServerWebExchange
- getRequest:ServerHttpRequest제공
- getResponse:ServerHttpResponse제공
- getAttributes:요청처리중에추가하거나변경할 수 있는 key-value로 구성된 Map 제공.
- 상태를 전달하거나 공유하는데 사용
- getSession: Session 정보를 담고 있는
- WebSessions publisher를 반환
- getPrincipal: Security 정보를 담고 있는
- Principal publisher를 반환
- getFormData: Content-Type이 application/x-www-form-urlencoded인 경우, MultiValueMap를 담고있는 publisher 제공
- getMultipartData: Content-Type이 multipart/form-data인 경우, body를 MultiValueMap 형태로 제공
- getApplicationContext: Spring 환경에서 구동된 경우 applicationContext 반환. Spring 환경이 아니라면 null
※ 본 포스팅은 패스트캠퍼스 환급 챌린지 참여를 위해 작성하였습니다. https://bit.ly/48sS29N