-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
[Java][Spring] do webflux controllers the right way #571
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
Java Technical Committee: cc @macjohnny |
|
It’s better to return a ResponseEntity to give the possibility for the implementer to set the Http status code and headers |
|
@cbornet that is wrong way cause with ResponseEntity you have to set http status code only once, before data is fetched and computed(before you know them). And cannot change them to status you need. But you can set them with WebExchange e.g.
After you fetch your data. And this is more flexible way than using ResponseEntity and exception handlers |
|
@ilya1st it might be helpful for reviewing to optimize the PR. could you please
|
|
@ilya1st I'm not saying that the current implementation is OK but that we should keep the ResponseEntity. So return |
|
Actually the ServerWebExchange is already passed (we use it for the examples). Which is a good thing since it's the layer that gives the most flexibility (you can force the payload with untyped/raw data, etc...) |
|
@macjohnny I've reset branch to commit with mustache fixes. |
|
I've updated the Petstore samples via e5f222d. Let's see how it goes. |
|
@wing328 Note that there are still changes required on this PR |
|
@cbornet thanks for the heads-up. I'm just trying to resolve the Shippable CI error. Let me update the samples one more time to see how it goes. |
|
@cbornet We had to write our generator based on this generator class for our internal projects for webflux. There are some issues with wrong work of delegate methods and classes for example. There are to much issues to make PRs for them. For webflux case code looks slack-baked. May be better idea for springboot webflux case write separate generator from scratch? For now templates are too overloaded with cases to prevent erros there. |
|
@cbornet Is support for |
|
@IsaacDeLaRosa see #913 . As for the gradle part, have you tried the gradle plugin ? |
|
Workaround with the current templates : if you need to set the status code or the headers reactively, you can do so by setting them on the @Override
public ResponseEntity<Mono<Void>> deletePet(Long petId, String apiKey, ServerWebExchange exchange) {
Mono<Void> result = Mono.empty()
.doOnSuccess(it -> exchange.getResponse().setStatusCode(HttpStatus.FORBIDDEN))
.doOnSuccess(it -> exchange.getResponse().getHeaders().add("foo", "bar"))
.then();
return ResponseEntity.status(HttpStatus.OK).body(result);
} |
|
Chiming in here in agreement with the OP. We've been looking at the generated Spring Web Reactive interfaces and the response type wrappings seem superfluous, this is using version
From all of our testing this behaves identically to Is there a reason for this or can it be simplified to |
|
The same about ResponseEntity could be said for the non-reactive generation. I agree that it could be an option not to wrap into ResponseEntity. |
|
Thanks for the quick response @cbornet, is this something you would be willing to take a PR for? If so would you want it conditional on option or just outright make the generator always create Flux and Mono response types? |
|
It shall be conditional with an option at least for backward compat. We can discuss on what should be the default. I don't know when I can find enough time for this... |
|
I'm closing this PR as the webflux gen has been fixed in another one. |
|
will do, cheers |
Hello, there is a problem with mustache templates for spring-boot-webflux reactive case.
ResponseEntity<Mono|Flux> is wrong way for this case.
Right way is to use @RestController annotation and return directly Flux or Mono objects from controllers.
Here is patchset to make generator do it right.