我是 Spring Webflux / Reactor Core 的新手,正在尝试执行以下功能:
调用 userservice.LoginWebApp()
如果返回用户,则返回类型为“User”的ResponseEntity。如果为空,则返回“String”类型的 ResponseEntity
以下代码给出类型错误,因为 .defaultIfEmpty() 需要 user 类型的 ResponseEntity。您能否建议实现此功能的正确操作员/方法。
@PostMapping("api/user/login/webApp")
public Mono<ResponseEntity> login(@RequestBody Credentials credentials, ServerWebExchange serverWebExchange) {
return userService.loginWebApp(credentials, serverWebExchange)
.map(user -> ResponseEntity.status(HttpStatus.OK).body(user))
.defaultIfEmpty(ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Invalid username or password"));
}
您可以使用cast
操作符从通用中向下转换,我相信 WebFlux 仍然能够编组User
和String
:
@PostMapping("api/user/login/webApp")
public Mono<ResponseEntity> login(@RequestBody Credentials credentials, ServerWebExchange serverWebExchange) {
return userService.loginWebApp(credentials, serverWebExchange)
.map(user -> ResponseEntity.status(HttpStatus.OK).body(user))
.cast(ResponseEntity.class)
.defaultIfEmpty(ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Invalid username or password"));
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)