Swagger Generation Automation or Creating the Bottling Line

High time we see our HelloBeerTM crafts hitting the shelves and start making a couple of bucks (Hail to capitalism!). Alas our project isn’t ready for that big step yet. We first need to crank up production a notch. So high time to automate our Swagger-to-Spring generation. In this blog post we’ll set up our Maven file to let it automatically generate the server stubs for our HelloBeer REST services.

As alway the complete code can be viewed and downloaded from GitHub.

Maven generation

Wouldn’t it be great if we could skip the manual code generation step performed in our previous blog posts and just have them Swagger Server Stub classes generated as part of our build process. Sure it would! And that’s exactly what the guys from SmartBear must have thought. Luckily they provided us with a Maven plugin – called swagger-codegen – to do the job.

The pom file will be based on the pom files we used in our previous blog posts. The most important addition will be said plugin and its configuration. So here it is:


<plugin>
  <groupId>io.swagger</groupId>
  <artifactId>swagger-codegen-maven-plugin</artifactId>
  <version>2.3.0</version>
  <executions>
    <execution>
      <goals>
        <goal>generate</goal>
      </goals>
      <configuration>
        <inputSpec>
                https://app.swaggerhub.com/apiproxy/schema/file/rphgoossens/hello-beer/1.0/swagger.yaml
        </inputSpec>
        <language>spring</language>
        <apiPackage>nl.whitehorses.hellobeer.generated.api</apiPackage>
        <modelPackage>nl.whitehorses.hellobeer.generated.model</modelPackage>
          <configOptions>
            <delegatePattern>true</delegatePattern>
          </configOptions>
          <generateSupportingFiles>false</generateSupportingFiles>
      </configuration>
    </execution>
  </executions>
</plugin>

The plugin is coupled to the generate goal. During generation the generated sources will by default be put in the ${project.build.directory}/generated-sources/swagger folder (this can be altered by setting the output parameter in the configuration).
The inputSpec parameter is pointing to the swagger.yaml file available on Swaggerhub. To get the exact location of the yaml file, just go to the “Download YAML (Unresolved)” menu item after selecting your API in SwaggerHub and its exact location will pop up in the bottom left corner.
The generateSupportingFiles has been disabled, so no pom or bootstrap class will be generated, as a matter of fact only the model and api classes will be generated with this setting in place.
The rest of the configuration options mirror the ones we explicitly set in SwaggerHub’s CodeGen Options in our previous blogs.

Upon issuing the mvn generate-sources phase, you’ll see the generated files appear in your target folder:
Screenshot from 2018-02-10 20-43-25

Implementation and tasting

Now that we’ve got our crafts flowing nicely through our production lines, it’s time to write the implementation. For this I just copied all the code I built up for my last non-swagger-server-stub-generation-automated project found here.

Tasting the beers can be done by issuing the same Postman calls like we did in our previous blogs. After startup a GET request will show the default beers we put in our inventory, just like it did in our non-automated version:

Screenshot from 2018-02-10 21-17-05

Summary

In this blog we’ve seen how easy it is to automatically generate the server stub using maven, making contract-first development with Swagger and Spring Boot a more than viable option.

In the next blog posts we’ll introduce some much needed functionality to our beer application: the client application aka the beer drinker. Cheers!

References

Advertisements

One thought on “Swagger Generation Automation or Creating the Bottling Line

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s