在Spring Webflux功能应用程序中验证请求的最佳方法是什么 [英] What is the best way to validate request in a Spring Webflux functional application

查看:170
本文介绍了在Spring Webflux功能应用程序中验证请求的最佳方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在传统的Web应用程序中,很容易使用控制器方法来验证请求主体,例如.

In a traditional web application it is easy to validate the request body in the controller method, eg.

ResponseEntity create(@Valid @ResponseBody Post post) {
} 

如果它是MVC应用程序,我们可以通过注入BindingResult来收集错误,并从输入表单中确定是否存在某些验证错误.

If it is a MVC application, we can gather the errors by injecting a BindingResult, and decide if there is some validation errors from the input form.

在页面中,存在一些Freemarker和Thymeleaf的助手来显示消息.

In the pages, there are some helpers existed for Freemarker and Thymeleaf to display the messages.

但是当我来到Webflux并尝试使用RouterFunction定义应用程序中的路由时.例如,

But when I come to Webflux and try to use RouterFunction to define the routing in the applications. For example,

Mono<ServerResponse> create(ServerRequest req) {
    return req.bodyToMono(Post.class)
    .flatMap { this.posts.save(it) }
    .flatMap { ServerResponse.created(URI.create("/posts/".concat(it.getId()))).build() }
}

@Bean
RouterFunction<ServerResponse> routes(PostHandler postController) {
    return route(GET("/posts"), postController.&all)
    .andRoute(POST("/posts"), postController.&create)
    .andRoute(GET("/posts/{id}"), postController.&get)
    .andRoute(PUT("/posts/{id}"), postController.&update)
    .andRoute(DELETE("/posts/{id}"), postController.&delete)
}

一种可能的方法是将请求数据(MonoFlux)转换为阻止并注入Validator并手动验证它们.

A possible approach is converting the request data(Mono or Flux) to blocking and injecting a Validator and validate them manually.

但是我认为代码看起来有些丑陋.

But I think the codes will look a little ugly.

如何<优雅地优雅地处理请求正文或表单数据的确认?

How to process the validation of request body or form data gracefully?

是否有更好的方法来验证请求正文或表单数据,并且不会丢失WEB(呈现视图)和REST应用程序的功能和响应功能?

Is there a better to validate the request body or form data and do not lost the functional and reactive features for both WEB(rendering a view) and REST applications?

推荐答案

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

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))));
}

这篇关于在Spring Webflux功能应用程序中验证请求的最佳方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆