且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Spring Webflux功能应用程序中验证请求的***方法是什么

更新时间:2023-01-09 22:58:19

我为此目的开发了另一个验证器".

I've developed "Yet Another Validator" for this porpose.

https://github.com/making/yavi

如果YAVI能够满足您的期望,那就太好了.

It would be great if YAVI could meet your expectation.

验证代码如下:

static RouterFunction<ServerResponse> routes() {
    return route(POST("/"), req -> req.bodyToMono(User.class) //
            .flatMap(body -> validator.validateToEither(body) //
                    .leftMap(violations -> {
                        Map<String, Object> error = new LinkedHashMap<>();
                        error.put("message", "Invalid request body");
                        error.put("details", violations.details());
                        return error;
                    })
                    .fold(error -> badRequest().syncBody(error), //
                          user -> ok().syncBody(user))));
}