spring-webflux

How to do post-matching filter in webflux?

可紊 提交于 2020-01-24 22:00:08
问题 As we know, in jersey we have pre-matching filter and post-matching filter. I'm wondering how can I get similar behavior with WebFilter in webflux application. It seems the WebFilter is sort of like a pre-matching filter which will be executed for sure, no matter a resource in @RestController found or not. My filter like this (copied from metrics filter in spring actuator): @Component @Order(100) public class AppFilter1 implements WebFilter { @Override public Mono<Void> filter

Reactive Spring does not support ServerHttpRequest as parameter in REST endpoint tests?

孤街浪徒 提交于 2020-01-24 15:36:05
问题 The question is very similar to this one. Except the fact that I use: org.springframework.http.server.ServerHttpRequest not HttpServletRequest. The exception is got in test code. Real calls works. Code: @RunWith(SpringRunner.class) @SpringBootTest(classes = SecurityTests.SecurityTestsApplication.class) @TestPropertySource(properties = {""}) @AutoConfigureWebTestClient public class SecurityTests { @Test public void myTest() { //send request to myUrl and got 500 } } @RestController

ReactiveSecurityContextHolder.getContext() is empty but @AuthenticationPrincipal works

本秂侑毒 提交于 2020-01-23 17:40:06
问题 I've been using ReactiveAuthenticationManager in Spring Security + Webflux. It is customised to return an instance of UsernamePasswordAuthenticationToken which from what I can tell is an what I should be receiving when I call ReactiveSecurityContextHolder.getContext().map(ctx -> ctx.getAuthentication()).block() . As as far as I can tell I am unable to access the authentication context through both: SecurityContextHolder.getContext().getAuthentication(); or ReactiveSecurityContextHolder

How to view response from Spring 5 Reactive API in Postman?

给你一囗甜甜゛ 提交于 2020-01-23 02:09:07
问题 I have next endpoint in my application: @GetMapping(value = "/users") public Mono<ServerResponse> users() { Flux<User> flux = Flux.just(new User("id")); return ServerResponse.ok() .contentType(APPLICATION_JSON) .body(flux, User.class) .onErrorResume(CustomException.class, e -> ServerResponse.notFound().build()); } Currently I can see text "data:" as a body and Content-Type →text/event-stream in Postman. As I understand Mono<ServerResponse> always return data with SSE(Server Sent Event) . Is

How to verify/test WebClient usage

风流意气都作罢 提交于 2020-01-22 15:15:22
问题 I need to unit test a class that uses the WebClient . Is there any good way to deal with the WebClient? With the RestTemplate I could easily use Mockito. Mocking the WebClient is a bit tedious, since deep stubs don't work with the webclient... I want to test if my code provides the correct headers... shortened sample code: public class MyOperations { private final WebClient webClient; public MyOperations(WebClient webClient) { this.webClient = webClient; } public Mono<ResponseEntity<String>>

How to verify/test WebClient usage

不羁的心 提交于 2020-01-22 15:15:09
问题 I need to unit test a class that uses the WebClient . Is there any good way to deal with the WebClient? With the RestTemplate I could easily use Mockito. Mocking the WebClient is a bit tedious, since deep stubs don't work with the webclient... I want to test if my code provides the correct headers... shortened sample code: public class MyOperations { private final WebClient webClient; public MyOperations(WebClient webClient) { this.webClient = webClient; } public Mono<ResponseEntity<String>>

what does Mono.defer() do?

大城市里の小女人 提交于 2020-01-20 02:20:27
问题 I've come across Mono.defer() in some Spring webflux code I looked up the method in the docs but don't understand the explanation: "Create a Mono provider that will supply a target Mono to subscribe to for each Subscriber downstream" please could I have an explanation and an example. Is there a place with a bunch of Reactor example code (their unit tests?) that I might reference. thanks 回答1: It is a bit of an oversimplification but conceptually Reactor sources are either lazy or eager. More

Flux.any correct use

╄→尐↘猪︶ㄣ 提交于 2020-01-16 14:19:39
问题 I have a Flux.fromIterable(list of ids) . I want to find out if any of the record is null .So I am trying to use Flux.any but I see that I does not even print anything inside any and directly goes to doOnEach as a result output is false. How can we fix this? The solution should not be limited to null check.It can be any boolean condition. Mono<Boolean> isAnyNull =Flux.fromIterable(request.getIds()) .switchIfEmpty(Mono.error(new SomeException("No elements"))) .flatMap(id->{ return FooRepo.find

How to run mono in the same thread, inside parallel flux

与世无争的帅哥 提交于 2020-01-16 08:38:12
问题 I'm trying to fill objects inside Flux with values from Mono. When i'm trying to do so, it's just ignoring my "set" operation. I assume that it's because Flux is working in parallel, while Mono is not. How can i solve this problem? Flux.fromIterable(proxyParserService.getProxyList()) .parallel() .runOn(Schedulers.parallel()) .filter(proxy -> proxy.getCorrupted() == null || !proxy.getCorrupted()) .subscribe(proxy -> { try { RestTemplate restTemplate = getProxiedTemplate(proxy.getHost(), proxy

How to upload mono of json and flux of files with webflux

…衆ロ難τιáo~ 提交于 2020-01-16 08:38:10
问题 I'm looking for a piece of code with Webflux that can handle a json part and a files stream. The request can upload a lot of files so I want to process one file at the time with zero copy by using a Flux (and not a flux on a preloaded Map). I think I tried everything and this is my last implementation that doesn't work well: This is my unit test: @Test @DisplayName("[TU] Http upload series") public void uploadSeries() { ArgumentCaptor<Mono<SeriesMetaDTO>> metaMono = ArgumentCaptor.forClass