3

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

Menelaos
  • 23,508
  • 18
  • 90
  • 155
  • Swagger generator is deprecated. Use form open api generator – hellboy Jan 26 '19 at 06:24
  • Swagger generator v2 is deprecated but there is a new v3 branch which is not. All the language options are available in the yaml UI website https://github.com/swagger-api/swagger-codegen/tree/3.0.0/modules/swagger-codegen-maven-plugin https://generator3.swagger.io/ui/ – Alex Dec 01 '20 at 13:37

1 Answers1

6

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 {
}
Eien
  • 1,057
  • 7
  • 11