2

我找到了项目https://github.com/swagger-api/swagger-codegen

但是,这会生成一个基于 OpenFeign 的客户端。

有没有办法自动生成使用 Netflix 的 feign 注释和请求映射的客户端接口?

例子:

@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);
}

与以下课程相反:

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

谢谢

4

1 回答 1

6

你可以试试spring-cloud swagger-codegen library

生成客户端的命令示例:

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

以下是生成文件的示例:

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 {
}
于 2018-04-13T19:35:53.690 回答