在Spring MVC框架中,当控制器返回接口类型时,spring的消息转换器可以将返回值转换为适当的消息(如JSON)。
例如,当我调用'vehicle‘api时,我可能会收到一条JSON消息。
@GetMapping("vehicle")
public Vehicle getVehicle() {
return new Car();
}
public interface Vehicle {
}
public class Car implements Vehicle {
private String gearType;
}但是,Webflux的行为是不同的。当返回类型是一个单声道接口时,它只支持'content- type : text/event-stream‘。
@GetMapping("vehicle")
public Mono<Vehicle> getVehicle() {
return Mono.just(new Car());
}在我看来,Webflux的行为是不舒服的。这是个bug吗?是否需要添加自定义MessageWriter来编码接口类型?
发布于 2019-04-22 19:00:07
在带注释的控制器中,返回一个Vehicle而不是Spring Docs中描述的Mono<Vehicle>就足够了。
注意:在使用Mono<T>或Flux<T>时,您可以使用显式处理程序函数,因为处理反应式类型时,处理程序函数更加显式。但是,它们的使用更加冗长。
https://stackoverflow.com/questions/55699729
复制相似问题