如果有手动添加的方法,Feign Client 不会代理接口继承的方法

问题描述 投票:0回答:1

在我的项目中,我有一个公共模块,其中包含根据 OpenAPI 规范生成的 API 接口:

@Generated(value = "org.openapitools.codegen.languages.SpringCodegen")
@Validated
@Tag(name = "Account Information", description = "Search and view customer accounts")
public interface AccountsApi {

    default Optional<NativeWebRequest> getRequest() {
        return Optional.empty();
    }

    @RequestMapping(
            method = RequestMethod.GET,
            value = "/accounts",
            produces = {"application/json"}
    )
    default ResponseEntity<Accounts> searchForAccounts(
            @Parameter(name = "accountIds", description = "Comma separated list of account ids", in = ParameterIn.QUERY) @Valid @RequestParam(value = "accountIds", required = false) List<String> accountIds
    ) {
        getRequest().ifPresent(request -> {
            for (MediaType mediaType : MediaType.parseMediaTypes(request.getHeader("Accept"))) {
                if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) {
                    String exampleString = "null";
                    ApiUtil.setExampleResponse(request, "application/json", exampleString);
                    break;
                }
            }
        });
        return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
    }
}

Feign 客户端有两个模块实现了该接口:

//module A
@FeignClient(name = "accountClient", url = "${accounts.internal.api.url}")
public interface AccountsClient extends AccountsApi {
}

//module B
@FeignClient(name = "accountClient", url = "${accounts.internal.api.url}")
public interface AccountsClient extends AccountsApi {

    @GetMapping
    ResponseEntity<Accounts> getAccounts(@RequestParam("customerXRef") String customerXRef);
}

我面临的问题是第二个客户端(在模块 B 中)仅代理对

getAccount()
的调用以及从
AccountsApi
继承的所有方法,例如(
AccountsApi.searchForAccounts()
) 返回 501 (
NOT_IMPLEMENTED
)。

我怎样才能解决这个问题并拥有继承方法的代理?

我使用 Spring Boot 2.7.17 和

org.springframework.cloud:spring-cloud-starter-openfeign:3.1.8

spring-cloud-feign feign openfeign
1个回答
0
投票

我通过引入中间接口封装

getAccounts()
方法解决了这个问题:

//module A
@FeignClient(name = "accountClient", url = "${accounts.internal.api.url}")
public interface AccountsClient extends AccountsApi {
}

//module B
public interface AccountsAdapter extends AccountsApi {

    @GetMapping
    ResponseEntity<Accounts> getAccounts(@RequestParam("customerXRef") String customerXRef);
}

@FeignClient(name = "accountClient", url = "${accounts.internal.api.url}")
public interface AccountsClient extends AccountsAdapter {
}

通过这种方法,模块 B 的

AccountsClient
工作得很好。

© www.soinside.com 2019 - 2024. All rights reserved.