我正在使用 spring boot 2.0.0.M2 遵循 Simon Basle 的“Reactor by Examples”中的代码。它就像一个魅力。但是,当我尝试将路径变量绑定(bind)到 Mono 并发送请求 http://localhost:8080/monohello/Hero 时,其响应如下:
{ "timestamp": 1500534277626, "status": 500, "error": "Internal Server Error", "message": "Failed to convert value of type 'java.lang.String' to required type 'reactor.core.publisher.Mono'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'reactor.core.publisher.Mono': no matching editors or conversion strategy found", "path": "/monohello/Hero" }
下面是 Controller :
@RestController public class ExampleController {
@GetMapping("hello/{who}")
public Mono<String> hello(@PathVariable String who) {
return Mono.just(who).map(w -> "Hello " + w + "!");
}
@GetMapping("monohello/{who}")
public Mono<String> monoHello(@PathVariable Mono<String> who) {
return who.map(w -> "Hello " + w + "!");
}}
最佳答案
您收到此错误的原因是 spring 找不到已注册的 Converter<String,Mono<String>>
。我不确定这实际上是您想要做的事情。在方法中创建 Mono 可能是最简单的。在这种情况下,使用转换器并没有获得太多好处。事实上,您主要只是失去了 Mono 创建方式的灵活性。
关于java - 无法通过@PathVariable绑定(bind)到Mono<String>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45208280/