This is an example on generating SpringFox/Swagger UI from API definition stored in a static JSON file.
Only Springfox dependency required in this example is the springfox-swagger-ui
JAR.
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger.version}</version>
</dependency>
A swagger.json
file containing the JSON definition is located under
resources/config
folder. The location of the file can be changed by
modifying swagger.json.location
property in application.yml
file.
A custom controller having the following endpoints to meet all the
requests from the swagger-ui.html
:
-
/swagger-resources
endpoint which returns a JSON string representation of aspringfox.documentation.swagger.web.SwaggerResource
object. -
/swagger-resources/configuration/ui
endpoint which returns a JSON string representation of aspringfox.documentation.swagger.web.UiConfiguration
object. -
/swagger-resources/configuration/security
endpoint which returns a JSON string representation of aspringfox.documentation.swagger.web.SecurityConfiguration
object. -
/v2/api-docs
endpoint which returns a JSON representation of a Swagger API definition.
To build the JAR, execute the following command from the parent directory:
mvn clean install
To run the application fromm command line,
java -jar target/springfox-ui-from-json-example-1.0.0.jar
You can view the Swagger UI at http://localhost:8080/swagger-ui.html
.
You can view Swagger JSON doc at http://localhost:8080/v2/api-docs?group=book