虚拟客户响应验证 [英] Feign client response validation

查看:69
本文介绍了虚拟客户响应验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个应用程序A和B使用FeignClient相互通信.作为应用程序A,我希望对应用程序B返回的数据进行验证.如果要验证请求参数,我可以轻松地使用@Valid批注并使用正确的spring验证批注对对象进行批注.那回应呢?

I have two applications A and B communicating with each other using FeignClient. As an application A I want to have validation on the data that is returned by application B. If I want to validate request parameters I can easily use @Valid annotation and annotate object with correct spring validation annotations. What about response ?

@FeignClient()
public interface AccountClient {
   @PostMapping("/accounts/account/create")
   void createAccount(@Valid CreateAccountRequest request);

   @PostMapping("/accounts/account/get")
   AccountResponse getAccount(AccountRequest request);

}

public classs AccountResponse {
   @NotNull
   public String status;
}

以代码为例.我可以轻松地在应用程序B中验证CreateAccountRequest.但是AccountResponse呢?在这种情况下,@ NotNull无法正常工作.我宁愿避免获得响应并手动检查状态是否为!= null,因为我会有更多这样的字段.

Code as an example. I can easily validate CreateAccountRequest in application B. But what about AccountResponse? In that case @NotNull is not working. I would prefer to avoid getting the response and manually checking if status != null cause I would have much more fields like this.

推荐答案

在这种情况下,如果将 @Validated 放置在 AccountClient 接口上,然后再进行响应验证,则应该可以进行响应验证将 @Valid 添加到 getAccount 方法上.

In this case, the response validation should work if you place @Validated onto AccountClient interface and then @Valid onto getAccount method.

@Validated
@FeignClient()
public interface AccountClient {
   @PostMapping("/accounts/account/create")
   void createAccount(@Valid CreateAccountRequest request);

   @Valid
   @PostMapping("/accounts/account/get")
   AccountResponse getAccount(AccountRequest request);

}

这篇关于虚拟客户响应验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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