Deprecation warning: from JHipster 6.3.0, this module has been integrated in the main generator and shall not be used anymore.
JHipster module to generate client code from an OpenAPI/Swagger definition
This is a JHipster module, that is meant to be used in a JHipster application.
This module generates client code using Spring-Cloud FeignClients in your JHipster app from an OpenAPI/Swagger definition.
The generated FeignClient can be used in both Monolithic and Micro-service applications.
This module works for JHipster v5+ apps. For older JHipster version, use version 2.x of this module (branch v2.x-JHipster2-3-4)
This module works with both Swagger v2 and OpenAPI v3 definitions.
As this is a JHipster module, we expect you have JHipster and its related tools already installed:
To install this module:
npm install -g generator-jhipster-swagger-cli
To update this module:
npm update -g generator-jhipster-swagger-cli
Run:
yo jhipster-swagger-cli
then answer the questions. You have the possibility to store a client configuration for future regeneration (eg. if there is an API update). If you do so, next time you launch the module, you will have the choice to generate a new client or to reuse one or several stored configurations.
You can configure the generated FeignClients directly from the application.yml.
RequestInterceptor
beans are generated from the OpenAPI securitySchemes
and are only activated if relevant properties are set.
If the OpenAPI spec doesn't contain the securitySchemes
, then you will need to configure the clients by yourself (see spring-cloud doc for details.)
The basic auth RequestInterceptor is activated if <clientName>.security.<securityName>.username
is set.
petstore:
security:
basicAuth:
username: admin
password: admin
The API key RequestInterceptor is activated if <clientName>.security.<securityName>.key
is set.
petstore:
security:
apiKey:
key: 12345
The OAuth2 RequestInterceptor is activated if <clientName>.security.<securityName>.key
is set.
For details on configuring OAuth2, see the spring-security-oauth2 doc.
petstore:
security:
passwordOauth:
client-id: myClientId
client-secret: myClientSecret
username: myUsername
password: myPassword
scopes:
- read
- write
The remote URL will default to the one from the OpenAPI spec but can be changed with the <clientName>.url
property.
petstore:
url: http://petstore-uat.swagger.io/v2
You need to add spring-cloud-starter-ribbon to your pom.xml if needed.
Note that it seems to cause an issue with form-login on monoliths so it is not done by this module.
Then set <cliName>.url
to blank.
petstore:
url:
ribbon:
listOfServers: petstore1.swagger.io,petstore2.swagger.io
If the generated clients don't fit your needs because you want to use Hystrix fallbacks, change the Ribbon context path or use different Feign client configuration, then simply create your own FeignClient beans extending the generated xxxApi classes.
For instance if you generated the petstore API, you can call the addPet method like this:
@Inject
private PetApiClient petApiClient;
...
Pet myPet = new Pet();
petApiClient.addPet(myPet);
Apache-2.0 © [Christophe Bornet]