Search code examples
javaspring-bootswagger-uispringfoxswagger-codegen

Springfox Swagger Ui from existing JSON


I have a gradle backed SpringBoot application where we were originally just creating our
REST controllers and then using the @EnableSwagger2 annotation to create our swagger UI. This was working just fine, but now we want to transition to an API first approach.

So this means we are now creating our swagger.json first, then using swagger-codegen to produce generate our Java interfaces / controllers for use in our SpringBoot application. I am now trying to figure out how to just tell springfox-swagger-ui to use the swagger.json we have created, instead of looking through the java code to produce the UI after the fact. It seems to me this is the correct approach, to avoid any inconsistencies that could possibly arise from essentially springfox-swagger-ui from regenerating the swagger.json behind the scenes.

How do I go about configuring this behavior? Thanks.


Solution

  • Here is a working example on how to create a Swagger UI from an existing swagger.json.

    You need to write a custom Swagger controller having the following endpoints:

    1. /swagger-resources endpoint which returns a JSON string representation of a springfox.documentation.swagger.web.SwaggerResource object.

    2. /swagger-resources/configuration/ui endpoint which returns a JSON string representation of a springfox.documentation.swagger.web.UiConfiguration object.

    3. /swagger-resources/configuration/security endpoint which returns a JSON string representation of a springfox.documentation.swagger.web.SecurityConfiguration object.

    4. /v2/api-docs endpoint which returns a JSON representation of a Swagger API definition.