Generate Swagger from a JSON to keep clean my .jav

2019-07-26 21:47发布

问题:

I am using Springfox v 2.7 and I'd like to generate my Swagger UI from a JSON file, to keep my Controller classes clean, and without annotations from Swawgger (@Api, @ApiDocs) either my model classes without any annotation (@ApiModelProperty, etc)....

Regards!

PD: This is a Spring boot 1.5.8 version App.

回答1:

Here are the changes you need to make:

  • Only Springfox JAR needed is springfox-swagger-ui JAR which contains all the static UI artifacts.

  • A swagger.json file containing all the JSON definition located under the resources folder.

  • A custom controller having the following endpoints to meet all the requests from the swagger-ui.html:

    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.

You can find a working example here.