spring-webflux

Spring webflux, testing `ServerResponse`

夙愿已清 提交于 2019-12-19 03:38:24
问题 How can I test a handler method that receive ServerRequest and return Mono<ServerResponse> ? I can create a ServerRequest via org.springframework.mock.web.reactive.function.server.MockServerRequest.builder() . And assert on the ServerResponse.statusCode() . However I would like to test the body of this ServerResponse but there is no way to extract it. ServerResponse response = target.search(MockServerRequest.builder() .method(HttpMethod.GET) .build() ).block(); assertThat(response.statusCode(

How to collect paginated API responses using spring boot WebClient?

核能气质少年 提交于 2019-12-18 16:55:31
问题 I have a paginated response from an URL, I want to keep on hitting the next page URL which I get from the previous response and keep on collecting items till I don't have a "nextPage" URL in my response. How to achieve this in a reactive way using spring boot WebClient from WebFlux with out blocking? Request1: GET /items response: { items: [...] nextPage: "/items?page=2" } Request2: GET /items?page=2 response: { items: [...] nextPage: "/items?page=3" } Request3: GET /items?page=3 response: {

How to use Spring WebClient to make multiple calls simultaneously?

限于喜欢 提交于 2019-12-18 15:28:36
问题 I want to execute 3 calls simultaneously and process the results once they're all done. I know this can be achieved using AsyncRestTemplate as it is mentioned here How to use AsyncRestTemplate to make multiple calls simultaneously? However, AsyncRestTemplate is deprecated in favor of WebClient. I have to use Spring MVC in the project but interested if I can use a WebClient just to execute simultaneous calls. Can someone advise how this should be done properly with WebClient? 回答1: Assuming a

How to correctly read Flux<DataBuffer> and convert it to a single inputStream

断了今生、忘了曾经 提交于 2019-12-18 12:18:36
问题 I'm using WebClient and custom BodyExtractor class for my spring-boot application WebClient webLCient = WebClient.create(); webClient.get() .uri(url, params) .accept(MediaType.APPLICATION.XML) .exchange() .flatMap(response -> { return response.body(new BodyExtractor()); }) BodyExtractor.java @Override public Mono<T> extract(ClientHttpResponse response, BodyExtractor.Context context) { Flux<DataBuffer> body = response.getBody(); body.map(dataBuffer -> { try { JaxBContext jc = JaxBContext

Spring 5 Web Reactive - How can we use WebClient to retrieve streamed data in a Flux?

[亡魂溺海] 提交于 2019-12-18 08:58:14
问题 The current milestone (M4) documentation shows and example about how to retrieve a Mono using WebClient : WebClient webClient = WebClient.create(new ReactorClientHttpConnector()); ClientRequest<Void> request = ClientRequest.GET("http://example.com/accounts/{id}", 1L) .accept(MediaType.APPLICATION_JSON).build(); Mono<Account> account = this.webClient .exchange(request) .then(response -> response.body(toMono(Account.class))); How can we get streamed data (from a service that returns text/event

How to create Flux from Mono

ε祈祈猫儿з 提交于 2019-12-17 20:05:30
问题 I have a Mono A. The Object A contains two lists. I want to create direct two Flux. Is this possible without block()? Mono<A> a = ... ; Flux<AList1> a1 = Flux.fromIterable(a.block().getList1()); 回答1: Use Mono.flatMapMany() method: Flux flux1 = mono.map(A::getList1).flatMapMany(Flux::fromIterable); Flux flux2 = mono.map(A::getList2).flatMapMany(Flux::fromIterable); 来源: https://stackoverflow.com/questions/49190668/how-to-create-flux-from-mono

Enable CORS in Spring 5 Webflux?

醉酒当歌 提交于 2019-12-17 17:52:30
问题 How to enable CORS in a Spring 5 Webflux Project? I cannot find any proper documentation. 回答1: I had success with this custom filter: import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.http.server.reactive

How to set a timeout in Spring 5 WebFlux WebClient

倖福魔咒の 提交于 2019-12-17 10:48:12
问题 I'm trying to set timeout on my WebClient, here is the current code : SslContext sslContext = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build(); ClientHttpConnector httpConnector = new ReactorClientHttpConnector(opt -> { opt.sslContext(sslContext); HttpClientOptions option = HttpClientOptions.builder().build(); opt.from(option); }); return WebClient.builder().clientConnector(httpConnector).defaultHeader("Authorization", xxxx) .baseUrl(this.opusConfig

How to configure i18n in Spring boot 2 + Webflux + Thymeleaf?

南笙酒味 提交于 2019-12-14 04:25:30
问题 I just start a new project based on Spring boot 2 + Webflux. On upgrading version of spring boot and replace spring-boot-starter-web with spring-boot-starter-webflux classes like WebMvcConfigurerAdapter LocaleResolver LocaleChangeInterceptor are missing. How now can I configure defaultLocale, and interceptor to change the language? 回答1: Just add a WebFilter that sets the Accept-Language header from the value of a query parameter. The following example gets the language from the language query

how to retry Spring WebClient to retry the operation based on the response?

房东的猫 提交于 2019-12-14 04:22:00
问题 I've been learning spring webflux and got stuck into this one. I've made a request to REST API from Spring app using WebClient. I want to retry the request based on the response. lets say if the response has property status: 'not-ready' , then I need to retry the same operation after a second. I tried the following way, but not sure how to implement it public Flux<Data> makeHttpRequest(int page) { Flux<Data> data = webClient.get() .uri("/api/users?page=" + page) .retrieve() .bodyToFlux(Data