响应:状态406-弹簧

时间:2019-06-01 14:29:23

标签: spring kotlin controller request reactor-netty

当我请求时,我收到406的消息:

  

“ org.springframework.web.server.NotAcceptableStatusException:406   NOT_ACCEPTABLE \“找不到可接受的表示\” \ n \ tat   org.springframework.web.reactive.result.method.annotation.AbstractMessageWriterResultHandler.writeBody(AbstractMessageWriterResultHandler.java:156)

我不知道如何解决它。你能告诉我为什么它不起作用吗?

@PostMapping("/sign-in")
fun signIn(@RequestBody request: SignInRequest): Mono<ServerResponse> {
    return userService.findByEmail(request.email)
            .flatMap {
                return@flatMap if (it != null && passwordEncoder.encode(it.password.toString()) == request.password) {
                    println("ok")
                    ok().body(BodyInserters.fromObject(AuthResponse(jwtUtil.generateToken(it))))
                } else {
                    println("no ok")
                    notFound().build()
                }
            }
}

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。就是这样:

@PostMapping("/sign-in")
fun signIn(@RequestBody request: SignInRequest): Mono<ResponseEntity<*>> {
    return userService.findByEmail(request.email).map {
        return@map if (it != null && passwordEncoder.encode(request.password) == it.password) {
            ResponseEntity.ok(AuthResponse(jwtUtil.generateToken(it)))
        } else {
            ResponseEntity.status(HttpStatus.UNAUTHORIZED).build<String>()
        }
    }.defaultIfEmpty(ResponseEntity.status(HttpStatus.UNAUTHORIZED).build<String>())
}