1:首先添加maven依赖的jar,网上有的只给出1-2个jar,但是实际上以下的jar都是需要的
<!-- swagger -->
<dependency>
<groupId>com.mangofactory</groupId>
<artifactId>swagger-springmvc</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>com.mangofactory</groupId>
<artifactId>swagger-models</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>com.wordnik</groupId>
<artifactId>swagger-annotations</artifactId>
<version>1.3.11</version>
</dependency>
<!-- swagger-springmvc dependencies -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.5.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.5.4</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>15.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml</groupId>
<artifactId>classmate</artifactId>
<version>1.1.0</version>
</dependency>
2:新建一个SwaggerConfig的配置文件
package com.shuyu.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
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;
/**
* @ClassName: SwaggerConfig
* @Description: swagger初始化配置文件
* @author shuyu.wang
* @date 2017年10月17日 下午1:51:50
* @version V1.0
*/
@EnableSwagger
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());
}
private ApiInfo apiInfo() {
ApiInfo apiInfo = new ApiInfo("Test项目API", "spring-API swagger测试", "My Apps API terms of service",
"iot_wangshuyu@126.com", "web app", "My Apps API License URL");
return apiInfo;
}
}
3:将以上配置文件加载到Spring容器中
<!--整合 Swagger -->
<!-- 将 springSwaggerConfig加载到spring容器 -->
<bean class="com.mangofactory.swagger.configuration.SpringSwaggerConfig" />
<!-- 将自定义的swagger配置类加载到spring容器 -->
<bean class="com.shuyu.config.SwaggerConfig" />
<!-- 静态资源文件,不会被Spring MVC拦截 -->
<mvc:resources mapping="/swagger/**" location="/swagger/" />
以上代码中,最后一行是添加静态资源路径的,等一下添加Swagger UI文件的时候需要用到,这里我们可以提前放上。
4:controller层添加相应的注解
类注释:
@Controller
@RequestMapping(value="/test")
@Api(value="test",description="测试接口描述")
public class TestController {
方法注释:
@RequestMapping(value="/header")
@ResponseBody
@ApiOperation(value="根据header户信息",httpMethod="GET",notes="get user by id")
public Map<String, Object> getHeader() {
Map<String, Object> map=new HashMap<>();
map.put("header", getHeadersInfo());
map.put("user-agent", getUserAgent());
return map;
}
5:UI下载
在git上下载相应UIhttps://github.com/swagger-api/swagger-ui/tree/v2.2.10,一定不要下载高版本,高版本配置完成后页面会一直显示No operations defined in spec!
解压后将dist文件夹中所有的文件拷贝到webapp/swagger这里的swagger是作者自定义的你可以写为自己创建的目录。
修改index.html中的 http://petstore.swagger.wordnik.com/v2/swagger.json修改为自己项目路径+api-docs,
例如:http://localhost:8080/Test/api-docs:Test为项目名称。
这样就完成了相关整合,启动服务,在浏览器输入http://localhost:8080/Test/swagger/index.html#/
源码下载:http://download.csdn.net/download/wang_shuyu/10025982