使用 Swagger 生成 Netflix Feign 代码
Netflix Feign Code Generation using Swagger
我找到了项目https://github.com/swagger-api/swagger-codegen。
然而,这是生成一个基于 OpenFeign 的客户端。
有没有一种方法可以自动生成客户端界面,该界面使用带有请求映射的 Netflix 假注?
示例:
@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);
}
相对于 class 位于:
谢谢
你可以试试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 {
}
我找到了项目https://github.com/swagger-api/swagger-codegen。
然而,这是生成一个基于 OpenFeign 的客户端。
有没有一种方法可以自动生成客户端界面,该界面使用带有请求映射的 Netflix 假注?
示例:
@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);
}
相对于 class 位于:
谢谢
你可以试试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 {
}