Skip to content

Latest commit

 

History

History
164 lines (124 loc) · 6.48 KB

5. Consume-API-with-openapi-subgenerator.md

File metadata and controls

164 lines (124 loc) · 6.48 KB

Consume the workshop API with another JHipster application

Now, your API is ready to be used by the other applications.

The Openapi specifications - and JHipster - will help us to do that easily.

Create a new JHipster application

At the same level than the previous one was generated create a new folder consumeApiWorkshop:

mkdir consumeApiWorkshop && cd consumeApiWorkshop

And run JHipster

jhipster

We basically want a standard monolith application with all default values.

Which *type* of application would you like to create? Monolithic application (recommended for simple projects)
? What is the base name of your application? consumeApiWorkshop
? What is your default Java package name? io.github.openapiworkshop
? Do you want to use the JHipster Registry to configure, monitor and scale your application? No
? Which *type* of authentication would you like to use? JWT authentication (stateless, with a token)
? Which *type* of database would you like to use? SQL (H2, MySQL, MariaDB, PostgreSQL, Oracle, MSSQL)
? Which *production* database would you like to use? PostgreSQL
? Which *development* database would you like to use? H2 with disk-based persistence
? Do you want to use the Spring cache abstraction? Yes, with the Ehcache implementation (local cache, for a single node)
? Do you want to use Hibernate 2nd level cache? Yes
? Would you like to use Maven or Gradle for building the backend? Maven
? Which other technologies would you like to use? (Press <space> to select, <a> to toggle all, <i> to invert selection)
? Which *Framework* would you like to use for the client? Angular
? Would you like to use a Bootswatch theme (https://bootswatch.com/)? Default JHipster
? Would you like to enable internationalization support? Yes
? Please choose the native language of the application English
? Please choose additional languages to install (Press <space> to select, <a> to toggle all, <i> to invert selection)
? Besides JUnit and Jest, which testing frameworks would you like to use? (Press <space> to select, <a> to toggle all,
 <i> to invert selection)
? Would you like to install other generators from the JHipster Marketplace? No

Once the project is generated, please modify the following files .yo-rc.json "serverPort": "8081",

application-dev.yml

server:
   port: 8081

To be sure the server will not start on the same port than the first application.

Generate the client Feign configuration

In the same terminal launch the JHipster openapi-client sub-generator. This generator will generate, for you, all the configurations needed to call the API, from the api.yml file.

jhipster openapi-client

And answer to questions, like this:

Welcome to the JHipster OpenApi client Sub-Generator
? Where do you want to import your OpenAPI/Swagger specification from ? From the api.yml spec of an existing Jhipster
project
? Enter the path to the jhipster project root directory ../openApiWorkshop
? What is the unique name for your API client (please avoid using Java keywords) ? openApiWorkshop
? Do you want to save this config for future reuse ? Yes

After that, you will have a new folder in src/main/java named client with all the feignClient configuration generated.

cient folder

Fixes

For unknown reasons you need to do some fixes to complete this workshop.

From both Student and Teacher API Model

Remove this line import org.openapitools.jackson.nullable.JsonNullable;

Add the import for LocalDate import java.time.LocalDate;

Create a DummyResource

For test purpose you need to create a dummy resource which will call the workshop API. This resource is a dummy one, it's worked only like a proxy and do nothing more. "In real life", much more scenario exists and will be more complex than this one. Obviously a client application calls an API to use it a business scenario.

Create a new class in the rest folder src/main/java/PACKAGE/web/rest where PACKAGE is your package name.

@RestController
@RequestMapping("/api")
public class DummyResource {

    private DefaultApiClient defaultApiClient;

    public DummyResource(DefaultApiClient defaultApiClient) {
        this.defaultApiClient = defaultApiClient;
    }

    @GetMapping("/dummy")
    public ResponseEntity<List<Student>> getDummy() {
        return defaultApiClient.getAllStudents();
    }
}

This class calls the defaultApiClient.getStudents generated from the api.yml file.

Test it

Launch the application and access to the API menu (you know how now ;))

Test the dummy-resource!

As expected the call will... failed! And returns a 401 response. The only reason why, is that the workshop API is protected and you need to be login to call it.

401

Update the security config in the workshop API

Please do not try this at home, or at school!

Open the SecurityConfiguration.java in the workshop API (the first generated project) and permit all requests to /api/v1/**.

.authorizeRequests()
            .antMatchers("/api/authenticate").permitAll()
            .antMatchers("/api/v1/**").permitAll()

re-launch the application.

re-test it

Now the endpoint is not secure anymore, you could reach it with the dummy resource.

200

Congratulations, you have just consumed the API from a client application just by using both feign client and the api.yml file.

My job here is done!

Going further

For production, we can't hack the security as we did in the previous chapter (except for real reasons). To avoid that we could:

  • call the login API and get the token back and add it in a header.
  • Use Oauth2 as authentication mode.

Learn more at Securing your application

Conclusion

I hope this workshop will help you to understand and master the API-First approach. In my opinion, we always should use this pattern to develop APIs. Using existing specifications is a real gain: reuse it for documentation, to make it available to our clients, for API management etc... And guess what? Your API will be generic enough to be used by everyone.

As we can see, JHipster help us to build it quickly, easily and with a strong code quality.

This workshop is always in progress and you can contribute if you want to improve it or fix something.

Thanks to have read it and have fun with JHipster ;).