在pom.xml中加入依赖html
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.7.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.7.0</version> </dependency>
增长JavaConfig配置文件java
@Configuration @EnableWebMvc @EnableSwagger2 public class SwaggerConfig { @Bean public Docket buildDocket() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(buildApiInf()) .host("localhost:8080") .select() .apis(RequestHandlerSelectors.basePackage("com.dianping.orderdish.webapi"))//controller路径 .paths(PathSelectors.any()).build(); } private ApiInfo buildApiInf() { return new ApiInfoBuilder() .title("RestAPI Docs") .termsOfServiceUrl("http://www.github.com/kongchen/swagger-maven-plugin") .build(); } }
若是Spring MVC
代理了根路径(\
)的url,在spring-mvc.xml
增长配置项。git
<!-- Enables swgger ui--> <mvc:default-servlet-handler/> <mvc:resources location="classpath:/META-INF/resources/" mapping="swagger-ui.html"/> <mvc:resources location="classpath:/META-INF/resources/webjars/" mapping="/webjars/**"/>
启动Tomat,经过localhost:8080/swagger-ui.html
访问Swagger-UI。github
<plugin> <groupId>com.github.kongchen</groupId> <artifactId>swagger-maven-plugin</artifactId> <version>3.1.5</version> <configuration> <apiSources> <apiSource> <springmvc>true</springmvc> <locations>com.example.demo13</locations><!-- Controller所在的位置 --> <host>orderdish-ecom-web.51ping.com</host> <basePath>/s</basePath> <schemes>http,https</schemes> <info> <title>${artifactId}</title> <version>v1</version> <description>Click Link Below for Help</description> <termsOfService>http://www.github.com/kongchen/swagger-maven-plugin</termsOfService> </info> <!--html文档输出功能的模板文件 --> <!--<templatePath>${basedir}/src/test/resources/swagger_template/strapdown.html.hbs</templatePath>--> <!--html文档输出的位置 --> <!--<outputPath>${project.build.directory}/swagger-ui/swagger_document.html</outputPath>--> <swaggerDirectory>${project.build.directory}/swagger-ui</swaggerDirectory><!--定义API描述文档的输出目录 --> <outputFormats>yaml,json</outputFormats><!--支持yaml和json格式 --> </apiSource> </apiSources> </configuration> <executions> <execution> <phase>compile</phase> <goals> <goal>generate</goal> </goals> </execution> </executions> </plugin>
执行mvn complie
,在设置的target
目录下会生成API描述文档。web
<plugin> <groupId>io.github.swagger2markup</groupId> <artifactId>swagger2markup-maven-plugin</artifactId> <version>1.3.3</version> <configuration> <!--The URL or file path to the Swagger specification--> <swaggerInput>${project.build.directory}/swagger-ui/swagger.yaml</swaggerInput> <outputDir>${project.build.directory}/swagger-ui</outputDir> <outputFile>${project.build.directory}/swagger-ui/swagger.md</outputFile> <config> <!--设置输出文件的语言:ASCIIDOC, MARKDOWN, CONFLUENCE_MARKUP--> <swagger2markup.markupLanguage>MARKDOWN</swagger2markup.markupLanguage> <!--设置目录的展示方式--> <swagger2markup.pathsGroupedBy>TAGS</swagger2markup.pathsGroupedBy> </config> </configuration> <executions> <execution> <phase>compile</phase> <goals> <goal>convertSwagger2markup</goal> </goals> </execution> </executions> </plugin>
若是mvn complie
时出现异常,多是由于以前生成的yaml不符合Swagger规范。把yaml复制到Swagger Editor
中,根据提示的语法错误修正,再手动执行mvn swagger2markup:convertSwagger2markup
便可生成markdown文档。spring