Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 31 |
Tags
- bootstrap
- JavaFX
- RSocket
- xml
- mybatis
- Python
- swift
- Ajax
- Linux
- Apache
- node.js
- 개발자 이야기
- maven
- 일본
- Spring
- build tool
- php
- gradle
- pdo
- SQLite
- javadoc
- webflux
- WebClient
- Java
- angularjs
- SQL
- 외국인 노동자
- Eclipse
- jquery
- spring boot
- Today
- 122
- Total
- 3,456,555
슬기로운 개발자 생활
[Spring Web Reactive] 2. WebClient - 2.3. Exchange 본문
IT 개발/Spring Web Reactive
[Spring Web Reactive] 2. WebClient - 2.3. Exchange
개발자 너부리 2021. 4. 12. 16:12반응형
exchangeToMono()
메소드와 exchangeToFlux()
메소드 (또는 Kotlin에서 awaitExchange { }
와 exchangeToFlow { }
)는 응답 상태에 따라 다르게 응답을 디코딩하는 등의 고급 제어가 필요한 고급 케이스에 도움이 된다.
Java
Mono<Object> entityMono = client.get()
.uri("/persons/1")
.accept(MediaType.APPLICATION_JSON)
.exchangeToMono(response -> {
if (response.statusCode().equals(HttpStatus.OK)) {
return response.bodyToMono(Person.class);
}
else if (response.statusCode().is4xxClientError()) {
// Suppress error status code
return response.bodyToMono(ErrorContainer.class);
}
else {
// Turn to error
return response.createException().flatMap(Mono::error);
}
});
Kotlin
val entity = client.get()
.uri("/persons/1")
.accept(MediaType.APPLICATION_JSON)
.awaitExchange {
if (response.statusCode() == HttpStatus.OK) {
return response.awaitBody<Person>()
}
else if (response.statusCode().is4xxClientError) {
return response.awaitBody<ErrorContainer>()
}
else {
throw response.createExceptionAndAwait()
}
}
위를 사용하는 경우, 반환된 Mono
또는 Flux
가 완료된 후에, 응답 본문을 확인하고 사용되지 않았으면 메모리와 연결 누수가 방지하기 위해 해제된다. 따라서 응답은 더 이상 다운스트림으로 디코딩 할 수 없다. 필요에 따라 응답을 해독하는 방법을 선언하는 것은 제공된 함수 달려 있다.
반응형
'IT 개발 > Spring Web Reactive' 카테고리의 다른 글
[Spring Web Reactive] 2. WebClient - 2.4. Request Body (0) | 2021.04.12 |
---|---|
[Spring Web Reactive] 2. WebClient - 2.4. Request Body (0) | 2021.04.12 |
[Spring Web Reactive] 2. WebClient - 2.2. retrieve() (0) | 2021.04.12 |
[Spring Web Reactive] 2. WebClient - 2.1. 구성 (0) | 2021.04.12 |
[Spring Web Reactive] 2. WebClient (0) | 2021.04.12 |
0 Comments