微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

具有不同响应类的 FeignClient API

如何解决具有不同响应类的 FeignClient API

我正在使用 Jhipster。我有一个 yaml 文件,然后使用 jhipster openapi-client 生成 java 代码。它生成几个文件包括所有需要的模型类(包含请求和响应)。

Defaultapiclient

@FeignClient(name="${default.name:default}",url="${default.url:https://test.api.com/testing}",configuration = ClientConfiguration.class)
public interface Defaultapiclient extends DefaultApi {
}

DefaultApi

@javax.annotation.Generated(value = "org.openapitools.codegen.languages.SpringCodegen",date = "2021-01-22T14:50:31.377193700+08:00[Asia/Singapore]")

@Validated
@Api(value = "Default",description = "the Default API")
public interface DefaultApi {

    /**
     * POST /req/v1 : This is the request
     *
     * @param authorization JWT header for authorization (required)
     * @param body  (required)
     * @return successful operation (status code 200)
     *         or server cannot or will not process the request (status code 400)
     */
    @ApiOperation(value = "This is the request",nickname = "Verification",notes = "",response = ResponseType.class,authorizations = {
        @Authorization(value = "clientID")
    },tags={  })
    @ApiResponses(value = {
        @ApiResponse(code = 200,message = "successful operation",response = ResponseType.class),@ApiResponse(code = 400,message = "server cannot or will not process the request",response = ServiceMessagesType.class) })
    @RequestMapping(value = "/req/v1",produces = "application/json",consumes = "application/json",method = RequestMethod.POST)
    ResponseEntity<ResponseType> Verification(@ApiParam(value = "JWT header for authorization",required=true,defaultValue="Bearer REPLACE_THIS_KEY") @RequestHeader(value="Authorization",required=true) String authorization,@ApiParam(value = "",required=true )  @Valid @RequestBody RequestType body);

}

我可以设法成功获得响应,但是当我发送错误请求时会出现问题,它将响应错误请求 400 并导致我的程序崩溃。

正如您在 swagger 注释@ApiResponse 上看到的那样,它返回不同的类。 我真的很困惑。我的问题是:

  1. 只是为了确认,@ApiResponse 仅用于文档,对吗?这段代码对程序有没有影响,比如返回码400,响应自动是ServiceMessageType类?
  2. 如何处理不同的响应类?正如您在函数定义 ResponseEntity Verification 中看到的那样,它将返回 ResponseType 作为 ResponseEntity 的主体。但是当我向这个 Api 发送错误请求时,这个 Api 会返回 ServiceMessageType。仅供参考,代码 400 会给我的程序一个错误,说“失败,没有可用的后备”,所以我想我需要一个错误句柄来做到这一点。
  3. 对于2号,我已经在几个来源中搜索解决方https://programmer.group/feign-call-error-failed-and-no-fallback-available.html 但我真的不明白。我使用的是 fallbackFactory,它可以处理 400 代码异常。但我仍然对如何返回不同的响应类感到困惑。正如链接所说,我得到的结果结构不正确:

通过实现FallbackFactory,可以在create方法获取服务抛出的异常。不过需要注意的是,这里的异常是由Feign封装的,在异常信息中是无法直接看到原方法抛出的异常的。获取到的异常信息如下: status 500 reading TestService#addRecord(ParamVO);内容:{"success":false,"resultCode":null,"message":"/ by zero","model":null,"models":[],"pageInfo":null,"timelineInfo":null,"extra":null,"validationMessages":null,"valid":false}

为了说明,在这个例子中,服务提供者的接口返回信息将统一封装在用户定义的类Result中,内容就是上面的内容:{"success":false,"valid ":false}

请向我解释它是如何工作的,或者您可以给我一个关于它如何工作的链接,我将非常感谢您的帮助。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。