javaspringkotlinspring-cloud-feign

How to auto generate Feign client in java\kotlin based on api interface?


I have that api interface for my next controller in spring boot

@RequestMapping("/v1/resource")
@Validated
interface ResourceApi {

    @Operation(summary = "get resource")
    @PostMapping("/get")
    fun getResource(
        @RequestBody request: GetRequest,
    ): PageableResult<GetResponse>

    @Operation(summary = "upsert resource")
    @PostMapping("/upsert")
    fun upsertResource(
        @RequestBody request: UpsertRequest,
    ): PageableResult<UpsertResponse>
}

I need auto generate somehow Feign client for that api like that

@FeignClient(
    contextId = "resourceClient",
    name = "service",
    path = "/v1/resource"
)
interface ResourceClient {

    @PostMapping("/get")
    fun getResource(
        @RequestBody request: GetRequest
    ): PageableResult<UpsertResponse>

    @PostMapping("/upsert")
    fun upsertResource(
        @RequestBody request: UpsertRequest
    ): PageableResult<UpsertResponse>
}

I am not interested on openapi generation like that guide or etc

Can OpenApi Generator generate interface feign clients?


Solution

  • You can solve that maintain based on basic inheritance. Also you must not describe @RequestMapping in api class due to error creating bean then feign client will be initialized

    My code works nice with that:

    @Validated
    interface ResourceApi {
    
        @Operation(summary = "get resource")
        @PostMapping("/v1/resource/get")
        fun getResource(
            @RequestBody request: GetRequest,
        ): PageableResult<GetResponse>
    
        @Operation(summary = "upsert resource")
        @PostMapping("/v1/resource/upsert")
        fun upsertResource(
            @RequestBody request: UpsertRequest,
        ): PageableResult<UpsertResponse>
    }
    

    And Feign client

    @FeignClient(
        contextId = "resourceClient",
        name = "service"
    )
    interface ResourceClient : ResourceApi