繁体   English   中英

假装客户端响应验证

[英]Feign client response validation

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

@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 不起作用。 我宁愿避免获得响应并手动检查 status.= null 因为我会有更多这样的字段。

在这种情况下,如果将@Validated放在AccountClient接口上,然后将@Valid放在getAccount方法上,响应验证应该会起作用。

这是标准的 Spring 验证功能,不仅适用于 Feign

import org.springframework.validation.annotation.Validated;
import javax.validation.Valid;

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

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

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM