【问题标题】:Feign client response validationFeign 客户端响应验证
【发布时间】:2021-08-30 17:28:00
【问题描述】:

我有两个应用程序 A 和 B 使用 FeignClient 相互通信。 作为应用程序 A,我想对应用程序 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 因为我会有更多这样的字段。

【问题讨论】:

    标签: java spring validation annotations feign


    【解决方案1】:

    在这种情况下,如果将@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);
    
    }
    

    【讨论】:

      猜你喜欢
      • 2018-02-12
      • 2019-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-08
      • 1970-01-01
      • 1970-01-01
      • 2021-12-08
      相关资源
      最近更新 更多