服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - Spring WebFlux实现参数校验的示例代码

Spring WebFlux实现参数校验的示例代码

2021-11-09 11:53lzyer Java教程

请求参数校验,在实际的应用中很常见,网上的文章大部分提供的使用注解的方式做参数校验。本文主要介绍 Spring Webflux Function Endpoint 使用 Spring Validation 来校验请求的参数。感兴趣的可以了解一下

请求参数校验,在实际的应用中很常见,网上的文章大部分提供的使用注解的方式做参数校验。本文主要介绍 Spring Webflux Function Endpoint 使用 Spring Validation 来校验请求的参数。使用上一篇文章的示例来演示。

使用步骤如下:

1.创建校验器 Validator

2.运用校验器

3.抛出异常,返回 http status 400 错误

PersonValidator.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package com.example.springbootdemo.webflux.restful;
 
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;
 
@Component
public class PersonValidator implements Validator {
 
  @Override
  public boolean supports(Class<?> clazz) {
    return Person.class.isAssignableFrom(clazz);
  }
 
  // 校验参数的方法
  @Override
  public void validate(Object o, Errors errors) {
    ValidationUtils.rejectIfEmpty(errors, "name", "name.required");
    ValidationUtils.rejectIfEmpty(errors, "age", "age.required");
    Person p = (Person) o;
    if (p.getAge() != null && p.getAge() < 0) {
      errors.rejectValue("age", "negative.value");
    } else if (p.getAge() != null && p.getAge() > 200) {
      errors.rejectValue("age", "too.old");
    }
  }
}

校验器在 savePerson 方法中的使用

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
@Slf4j
@Component
public class PersonHandler {
 
  @Autowired
  private PersonRepository repository;
  @Autowired
  private PersonValidator validator;
 
  public Mono<ServerResponse> savePerson(ServerRequest request) {
    Mono<Person> personMono = request.bodyToMono(Person.class).doOnNext(this::validate);
    return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON)
        .body(this.repository.savePerson(personMono), Void.class);
  }
    
  public void validate(Person person) {
    Errors errors = new BeanPropertyBindingResult(person, Person.class.getName());
    validator.validate(person, errors);
    if (errors.hasErrors()) {
        // 抛出 http status 400 异常
      throw new ServerWebInputException(errors.toString());
    }
  }
    // .... 省略
}

请求效果:

Spring WebFlux实现参数校验的示例代码

官方校验参数示例的地址 https://docs.spring.io/spring-framework/docs/current/reference/html/web-reactive.html

使用 Spring 官方文档提供的示例不会抛出 http code 400 错误,返回的是http code 为 200。

接下来,我们来看一下Validator 接口中的两个方法 supportsvalidate

  • supports(Class) : 判断当前的校验器用指定的类上。
  • validate(Object, org.springframework.validation.Errors) : 校验给定的对象,如果出现错误,就给Errors 注册 Error 信息。

另外,Spring 还提供了非常好用的 ValidationUtils 的工具类,提供了静态的方法

  • rejectIfEmpty
  • rejectIfEmptyOrWhitespace

全局异常的使用

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
@Configuration
@Slf4j
public class GlobalErrorConfig {
 
  private ObjectMapper objectMapper = new ObjectMapper();
 
  @Bean
  @Order(-2)
  public WebExceptionHandler exceptionHandler() {
    return (ServerWebExchange serverWebExchange, Throwable t) -> {
      DataBuffer dataBuffer = serverWebExchange.getResponse().bufferFactory().allocateBuffer();
      Result result = new Result();
      if (t instanceof ServerWebInputException) {
        ServerWebInputException exception = (ServerWebInputException) t;
        result.setCode(exception.getStatus().value());
        result.setMessage(exception.getReason());
      } else {
        result.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
        result.setMessage(HttpStatus.INTERNAL_SERVER_ERROR.toString());
      }
      try {
        dataBuffer.write(objectMapper.writeValueAsBytes(result));
      } catch (JsonProcessingException e) {
        log.error(NestedExceptionUtils.buildMessage("write error", e));
      }
      ServerHttpResponse response = serverWebExchange.getResponse();
      response.setRawStatusCode(result.getCode());
      return response.writeWith(Mono.just(dataBuffer));
    };
  }
}

Result.java

?
1
2
3
4
5
6
7
8
9
10
11
import lombok.Getter;
import lombok.Setter;
 
@Getter
@Setter
public class Result {
 
  private Integer code;
 
  private String message;
}

请求效果:

Spring WebFlux实现参数校验的示例代码

至此,Webflux 的Function Endpoint 的参数校验的使用结束了。

参考:

WebFlux Handler Validation

Spring Validator

到此这篇关于Spring WebFlux实现参数校验的示例代码的文章就介绍到这了,更多相关Spring WebFlux 参数校验内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://www.cnblogs.com/lzeffort/p/15112774.html

延伸 · 阅读

精彩推荐