Build Reactive APIs with Spring WebFlux

Jocelyn Lecomte

I am struggling a little bit to understand your test for ProfileService.all().
It seems to me that you first get all profiles (from an empty repository) and then save some profiles. I think you should first save some profiles, then verify that ProfileService.all() gets you the proper result.
As the code is, the test is green because it happens that repository.saveAll() returns the same result,…
Did I miss something ?

Matt Raible

The test is making sure that the service and the repository are passed the same data. Steps 4-6 in the test’s code listing have more information.

Nadeem

I followed steps to example 18 and the server runs without error but when i open html page it show websocket connection failed.

Matt Raible

You might try comparing your code to the example code on GitHub. We also have a couple newer tutorials that are somewhat related.

- Tutorial: Develop Apps with Secure WebSockets in Java
- R2DBC and Spring for Non-Blocking Database Access

Nadeem

I recreated using this youtube video as a source upto 24.09 and now I’m getting an error javax.servlet.ServletException: No adapter for handler [com.reactiveWeb.WebSocketConfiguration$$Lambda$1133/0x0000000100782728@76495257]: The DispatcherServlet configuration needs to include a HandlerAdapter that supports this handler, and cause of that websocket connection fails, please help me with this one I feel like I’m almost there

Matt Raible

It sounds like you might have an import that’s wrong. Since this is a reactive project, there shouldn’t be any servlets or ServletExceptions in it.

Nadeem

Thanks a lot for your help, really appreciate it…:blush:

roeniss

Your post is great! BTW can you help me?

Is there any difference or superiority between –

A: (your way)
private static String id(ServerRequest r) {
return r.pathVariable(“id”);
}

,

B:
private static Mono<string> id(ServerRequest r) {
return Mono.just(r.pathVariable(“id”));
}

, and

C:
private static Supplier<mono<string>> id(ServerRequest r) {
return ()->Mono.just(r.pathVariable(“id”)));
}

?

I just want to know which is the most reactive way!

Matt Raible

In my experience with reactive programming, the most verbose way (C) is the most reactive! :wink:

To be honest, I’m not sure. I’d suggest asking this question on Stack Overflow and and tagging it with “spring-webflux”.

roeniss

I’ll follow that. Thank you Matt. It’s so hard to find out best practices for Webflux in the internet… compared to MVC :frowning:

chr

I know this is a couple years old but did you ever resolve this? I just tried this out and then noticed this issue right away and then saw it was a known issue in the comments :(.

chr

NVM, I saw Nergal put the correct solution above.

rubn

@mattraible your awesome, I was looking because I could not update my item in a functional endpoint with RouterFunction, and thanks to reading you I already succeeded, thanks for this post.

And on top of that, you show an application that goes beyond any common tutorial.

1 Like

I had an issue when trying to make WebSocket work, I had to refresh the page at least once before starting consuming the record but I solve it by initializing the queue with one element.