Skip to content

luchob/swagger-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

35 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Table of contents

Welcome to the Swagger sample project!

Overview

This sample application focuses on swagger and its possible usage scenarios in Spring Boot porjects. CI is taken into account. Several scenarios are discussed here.

  • generating the Swagger API specification with springfox in a running server
  • using the Swagger UI which is built in the application
  • using the stand alone Swagger UI
  • generating static REST API documentation
  • generating swagger spec as build artifact
  • using the swagger spec in project automation

Starting the application

To start the application clone the project. It comes with gradlew wrapper so the only thing that should be executed from within the project directory is:

gradlew bootRun

This will start a server locally at port 8080.

Exploring the application (Eclipse)

The application may be imported in Eclipse as an existing project. To do so from the project directory execute:

gradlew eclipse

Open eclipse and import the project as existing java project.

Authentication

The REST API is currently secured with Basic Authentication. The application has one built in user with name lucho and password test.

Exploring the REST API with a built in Swagger UI client

Start the application. This application comes with a built in Swagger UI client. It is exposed at the following endpoint http://localhost:8080/swagger-ui.html.

This endpoint is normally not available in production system thus the application is configured to expose it only when the current active profile is dev. The application is by default started with this profile. The UI should look similar to the one below (at the time of writing).

image

Exploring the REST API with stand alone Swagger UI

Start the application. It is neither required nor even recommended to bundle Swagger UI with the application the standalone client might be used. Clone the Sagger UI. This may take some time. Change to swagger-ui\dist and open index.html within your browser. In the URL input at the top of the page enter the URL of the Swagger Specification exposed by the application. This should be http://localhost:8080/v2/api-docs. Click explore.

Generate static HTML documentation

Start the application. Download a binary distribution of swagger codegen CLI, for example from Maven. Note: check if newer version is available. Place the jar file in a directory of your choice and navigate to this directory. If the java environment is step correctly the following could be executed:

java -jar swagger-codegen-cli-2.2.2.jar generate -l html2 -i http://localhost:8080/v2/api-docs -o docs

The command will generate static HTML documentation and store it in the docs folder. All this is not as good as Swagger UI and looks like this at the time of writing:

image

Automation

Everything about springfox is nice but in order to generate a swagger specification the server needs to be running. This may not be perfect for CI. The swagger specification may be reqired in build time when there is no running server.

Generating Swagger specification with gradle at build time

At the time of writing there is no very good support for gradle. Maven is the favoured build system. There is a nice maven plugin. This project uses a gradle port of the plugin. To generate swagger specification execute:

gradlew build 
gradlew swagger

The swagger spec is available under the project root in \generated\swagger-json. The swagger spec may be used in various ways, e.g. it may be even tried out in the online swagger editor. It can be deployed as a release artifact.

Sample usage of the swagger spec

The project comes with a sample gradle plugin for swagger2markup which is able to generate Ascii Doc from the swagger specification. To see it in action execute:

gradlew build 
gradlew swagger
gradlew convertSwagger2markup -b swagger2makup.gradle

The output will be generated in under generated/asciidoc. Sample rendering:

image

Generating the swagger spec with an integration test

A mock MVC starts a minimalistic server which is capable enough to serve the swagger spec. A test case may be used to persist this spec. To try this out run:

gradlew clean test

The output will be normally generated under generated\swagger-test.

The Big Picture

Below is a diagram which may show "the big picture" emerging from all those usage scenarios above.

image

Feedback

Ideas? Issues?

About

A sample Spring Boot application exposing REST API documented with Swagger

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages