Skip to content
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

Return value doesn't serialize to JSON when return type is Mono of Interface in WebFlux #22803

Closed
freddieyyh opened this issue Apr 16, 2019 · 3 comments
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Milestone

Comments

@freddieyyh
Copy link

In WebMVC type application, Spring's message converter can convert the return value to a proper message(like a JSON) when controller return type declared by interface.

But, Webflux's behavior is different.

For example, we have Car class that implements Vehicle interface.

public interface Vehicle {
}

public class Car implements Vehicle {
    private String gearType;
}

And then there is a controller method like that.

@GetMapping("vehicle")
public Vehicle getVehicle() {
    return new Car();
}

I can get a json response of Car class with content-type: application/json header.

But, webflux's result is defferent.

@GetMapping("vehicle")
public Mono<Vehicle> getVehicle() {
    return Mono.just(new Car());
}

It only supports a 'content-type: text/event-stream' when return type is a mono of interface.
Is it a bug?

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Apr 16, 2019
@rstoyanchev
Copy link
Contributor

This odd. The signature with Mono<Vehicle> should work in both Spring MVC and WebFlux as expected. Do you have a sample?

@rstoyanchev rstoyanchev added status: waiting-for-feedback We need additional information before we can continue and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Apr 17, 2019
@freddieyyh
Copy link
Author

Here is my simple example.

Webflux : https://github.com/freddieyyh/demo/tree/webflux
WebMVC : https://github.com/freddieyyh/demo/tree/webmvc

Command curl -X GET -H "Accept: application/json" "localhost:8080/interface-a"
works on WebMVC demo, but doesn't work on Webflux demo.

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Apr 18, 2019
@rstoyanchev
Copy link
Contributor

The Result interface from the sample has no methods, so Jackson returns false for getObjectMapper().canSerialize(Result.class). Adding at least one accessor makes it work.

Spring MVC works is because it uses the actual type ResultA. We can make an improvement in WebFlux so that a concrete return value works the same, but for Mono<Result> it would still rely on the declared type, so it's best to have some sort of accessor on the interface.

@rstoyanchev rstoyanchev self-assigned this May 1, 2019
@rstoyanchev rstoyanchev added this to the 5.2 M2 milestone May 1, 2019
@rstoyanchev rstoyanchev added in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement and removed status: feedback-provided Feedback has been provided labels May 1, 2019
@rstoyanchev rstoyanchev changed the title Return value doesn't convert a return value to json when a controller's return type is Mono of Interface, in webflux Return value doesn't serialize to JSON when return type is Mono of Interface in WebFlux May 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

3 participants