Search code examples
springswagger-codegennetflix-feignspring-cloud-feignfeign

Netflix Feign Code Generation using Swagger


I found the project https://github.com/swagger-api/swagger-codegen .

However this is generating a client that is based on OpenFeign.

Is there a way to generate a client interface automatically that uses Netflix's feign annotation with request mappings?

Example:

@FeignClient(name = "ldap-proxy")
public interface LdapProxyClient  { 
    @RequestMapping(path = "/ldap-proxy/v1/users/{userNameOrEMail}", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.GET)
    LdapUser search(@PathVariable("userNameOrEMail") String userNameOrEMail);
}

As opposed to the class at:

https://github.com/swagger-api/swagger-codegen/blob/master/samples/client/petstore/java/feign/src/main/java/io/swagger/client/ApiClient.java

Thannks


Solution

  • You can try spring-cloud swagger-codegen library.

    The example of the command to generate the client:

    java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate \
         -i http://petstore.swagger.io/v2/swagger.json \
         -l spring \
         --library spring-cloud \
         -o samples/client/petstore/java
    

    Here is the example of the generated files:

    PetApi.java

    @Api(value = "Pet", description = "the Pet API")
    public interface PetApi {
    
        @ApiOperation(value = "Add a new pet to the store", nickname = "addPet", notes = "", authorizations = {
        @Authorization(value = "petstore_auth", scopes = {
            @AuthorizationScope(scope = "write:pets", description = "modify pets in your account"),
            @AuthorizationScope(scope = "read:pets", description = "read your pets")
            })
        }, tags={ "pet", })
        @ApiResponses(value = { 
        @ApiResponse(code = 405, message = "Invalid input") })
        @RequestMapping(value = "/pet",
        produces = "application/json", 
        consumes = "application/json",
        method = RequestMethod.POST)
        ResponseEntity<Void> addPet(@ApiParam(value = "Pet object that needs to be added to the store" ,required=true )  @Valid @RequestBody Pet body);
    }
    

    PetApiClient.java

    @FeignClient(name="${swaggerPetstore.name:swaggerPetstore}", url="${swaggerPetstore.url:http://petstore.swagger.io/v2}", configuration = ClientConfiguration.class)
    public interface PetApiClient extends PetApi {
    }