Swagger-API文档生成框架的基本使用

Swagger-API文档生成框架的基本使用

2.1.6 引入swagger-ui

pom.xml

io.springfox

springfox-swagger-ui

2.8.0

2.1.7 启动项目访问basePath/swagger-ui.html即可

在这一步,我本地出现以下问题,尝试了几种解决方式,但是暂时未解决,以后解决了会补上解决方式

Swagger-API文档生成框架的基本使用

在此暂时通过其他服务器部署好的swagger-ui来访问当前项目生成的数据

Swagger-API文档生成框架的基本使用

Swagger-API文档生成框架的基本使用

Swagger-API文档生成框架的基本使用

通过点击图上Try it out可以访问并测试接口功能,至此就完成了swagger的嵌入

2.2 第二种方式再介绍一下第二种方式

2.2.1 同理构建一个spring-web项目

2.2.2 pom引入

pom.xml

com.mangofactory

swagger-springmvc

1.0.2

com.mangofactory

swagger-models

1.0.2

com.wordnik

swagger-annotations

1.3.11

com.google.guava

guava

15.0

com.fasterxml.jackson.core

jackson-annotations

2.4.4

com.fasterxml.jackson.core

jackson-databind

2.4.4

com.fasterxml.jackson.core

jackson-core

2.4.4

com.fasterxml

classmate

1.1.0

2.2.3 SwaggerConfig 配置swagger

SwaggerConfigpackage com.swagger.config;

import com.mangofactory.swagger.configuration.SpringSwaggerConfig;

import com.mangofactory.swagger.models.dto.ApiInfo;

import com.mangofactory.swagger.plugin.EnableSwagger;

import com.mangofactory.swagger.plugin.SwaggerSpringMvcPlugin;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.ComponentScan;

import org.springframework.context.annotation.Configuration;

@Configuration

@EnableSwagger

@ComponentScan("com.swagger.controller") //启用组件扫描

public class SwaggerConfig {

private SpringSwaggerConfig springSwaggerConfig;

/**

* Required to autowire SpringSwaggerConfig

*/

@Autowired

public void setSpringSwaggerConfig(SpringSwaggerConfig springSwaggerConfig)

{

this.springSwaggerConfig = springSwaggerConfig;

}

/**

* Every SwaggerSpringMvcPlugin bean is picked up by the swagger-mvc

* framework - allowing for multiple swagger groups i.e. same code base

* multiple swagger resource listings.

*/

@Bean

public SwaggerSpringMvcPlugin customImplementation()

{

return new SwaggerSpringMvcPlugin(this.springSwaggerConfig)

.apiInfo(apiInfo())

.includePatterns(".*?");

}

private ApiInfo apiInfo()

{

ApiInfo apiInfo = new ApiInfo(

"Swagger测试接口",

"这是一个Swagger生成API测试",

"My Apps API terms of service",

"[email protected]",

"My Apps API Licence Type",

"My Apps API License URL");

return apiInfo;

}

}

2.2.4 其他Controller和Entity同理,构建完成启动即可

有些还不是很明白的是这种方式生成的地址是basUrl/api-docs,应该是对这个还不是很熟悉,之后如果有机会深入会继续记录分享给大家

Swagger-API文档生成框架的基本使用

将该接口放至开始服务器无法读取接口信息

所以尝试使用原始方式搭建一个swagger-ui

2.2.5 spring.xml

2.2.6 搭建swagger-ui

下载 swagger-ui

https://github.com/swagger-api/swagger-ui/tree/v2.2.10

解压下载后的文件,将里面dist文件夹下内容放置项目中

2.2.7 修改index.html内容

Swagger-API文档生成框架的基本使用

2.2.8 访问swagger-ui

访问项目下swagger中index.html,发现也可以达到效果

Swagger-API文档生成框架的基本使用

3.总结

总结下这次初步学习swagger,确实踩了很多坑,有些问题从网上找到的解决方法在自己这里也不管用。总结一下其实如果项目多,单独搭建一个提供查看接口文档的swagger-ui项目,其他项目主要负责生成对应数据格式文件,还是很方便的。有些小伙伴项目嵌入swagger可能会遇到一些小问题,比如生成的文档格式数据、或者原始方式引用swagger-ui被过滤器拦截导致接口生成数据无法读取成功等等。总结一下学习技术还是要静下心来,心越乱离成功越远。下面列一些学习过程中看到的不错的博客推荐给大家,分享给大家一起学习,这应该也是博主写博客的其中一个目的吧。

相关博客推荐:

[Swagger使用](https://blog.csdn.net/blucelee2/article/details/51140587)

[swagger的使用](https://blog.csdn.net/gyb_csdn/article/details/75123575)

[swagger编写规范](https://blog.csdn.net/xxoo00xx00/article/details/77278510)

[Swagger框架学习分享](https://blog.csdn.net/u010827436/article/details/44417637)

[swagger2常用注解说明](https://blog.csdn.net/u014231523/article/details/76522486)

[使用springfox+swagger2书写API文档](https://blog.csdn.net/u012476983/article/details/54090423)

[ SpringMVC+Swagger UI生成可视图的API文档(详细图解)](https://blog.csdn.net/u011499992/article/details/53455144)

[Restful形式接口文档生成之Swagger与SpringMVC整合手记](https://blog.csdn.net/linlzk/article/details/50728264)

[SSM三大框架整合Springfox(Swagger2)步骤以及遇到的一些问题](https://blog.csdn.net/twomr/article/details/77101092)


分享到:


相關文章: