Spring boot 如何在Spring WebClient中验证响应对象
我正在使用Springs的Webclient进行HTTP GET呼叫 如何验证作为HTTP调用响应接收的响应对象GetPersonBasicInfoResWrapper的属性 我试图通过阻塞response对象来验证Spring boot 如何在Spring WebClient中验证响应对象,spring-boot,spring-webflux,spring-webclient,Spring Boot,Spring Webflux,Spring Webclient,我正在使用Springs的Webclient进行HTTP GET呼叫 如何验证作为HTTP调用响应接收的响应对象GetPersonBasicInfoResWrapper的属性 我试图通过阻塞response对象来验证flatMap中的birthDate,但这看起来不是最有效的方法 以下是我的代码摘录 private Mono<GetPersonBasicInfoResWrapper> getPersonBasicInfo(Double personId, LocalDate birt
flatMap
中的birthDate
,但这看起来不是最有效的方法
以下是我的代码摘录
private Mono<GetPersonBasicInfoResWrapper> getPersonBasicInfo(Double personId, LocalDate birthDate,
CallerRequestMetaData callerInfo) {
return middlewareWebClient
.get()
.uri(...)
...
...
.exchange()
.flatMap(client -> {
GetPersonBasicInfoResWrapper block = client.bodyToMono(GetPersonBasicInfoResWrapper.class).block();
LocalDate personBirthDate = LocalDateTime.ofInstant(block.getBirthDate().toInstant(),ZoneId.of(Constants.DEFAULT_TIME_ZOME)).toLocalDate();
if (!personBirthDate.equals(birthDate)) {
throw new YakeenRowadException(Errors.INCORRECT_ID_BIRTH_DATE_G, birthDate.toString());
} else
return client.bodyToMono(GetPersonBasicInfoResWrapper.class);
});
}
private Mono getPersonBasicInfo(双人名,LocalDate birthDate,
CallerRequestMetaData(callerInfo){
返回middlewareWebClient
.get()
.uri(…)
...
...
.exchange()
.flatMap(客户端->{
GetPersonBasicInfoResWrapper block=client.bodyToNo(GetPersonBasicInfoResWrapper.class).block();
LocalDate personBirthDate=LocalDateTime.ofInstant(block.getBirthDate().toInstant(),ZoneId.of(Constants.DEFAULT_TIME_ZOME)).toLocalDate();
如果(!personBirthDate.equals(birthDate)){
抛出新的YakeenRowadeException(Errors.Errors_ID_BIRTH_DATE_G,birthDate.toString());
}否则
返回client.bodytomino(GetPersonBasicInfoResWrapper.class);
});
}
非常感谢您的帮助。试试这样的方法
private Mono<PersonInfo> getPersonInfo(Double personId) {
return webClient.get()
.uri(...)
.exchange()
.flatMap(response -> {
return response.bodyToMono(PersonInfo.class);
});
}
private LocalDate toLocalDate(Instant instant) {
return LocalDateTime.ofInstant(instant, ZoneId.of(Constants.DEFAULT_TIME_ZOME))
.toLocalDate();
}
public Mono<PersonInfo> doSomething(Double personId, LocalDate birthDate) {
return getPersonInfo(personId)
.flatMap(personInfo -> {
final LocalDate birthDate = toLocalDate(personInfo.getBirthDate().toInstant());
if (!personBirthDate.equals(birthDate)) {
return Mono.error(new YakeenRowadException(Errors.INCORRECT_ID_BIRTH_DATE_G, birthDate.toString()));
}
return Mono.just(personInfo);
});
}
private Mono getPersonInfo(双personId){
返回webClient.get()
.uri(…)
.exchange()
.flatMap(响应->{
返回响应bodytomino(PersonInfo.class);
});
}
私有LocalDate到LocalDate(即时){
返回LocalDateTime.ofInstant(instant,ZoneId.of(Constants.DEFAULT\u TIME\u ZOME))
.toLocalDate();
}
公共单剂量(双重人格,本地出生日期){
返回getPersonInfo(personId)
.flatMap(personInfo->{
最终LocalDate birthDate=toLocalDate(personInfo.getBirthDate().toInstant());
如果(!personBirthDate.equals(birthDate)){
返回Mono.error(新的YakeenRowadeException(Errors.Errors.ID\u BIRTH\u DATE\u G,birthDate.toString());
}
返回Mono.just(personInfo);
});
}
在获取期间不进行验证,验证是业务逻辑,应该在上面的层中
(尽量避免冗长的命名)不要
block()
在代码中,如果您使用block,您将失去使用webflux的所有目的。首先返回一个Mono
,然后通过调用Mono\flatMap
@ThomasAndolf进行验证!我也不想阻止,但我不确定如何以非阻止的方式实现验证。你能发布一些代码吗?这看起来很有用。我会测试并让你知道!!感谢来自OOPs
背景的您,很难理解这个功能范例。您可以在方法调用的第一行看到return
方法。然而,我喜欢这种新的范式。