新手入门Swagger看了很多博客,竟然没有一个是步骤齐全的或直接能运行的。于是CSDN下载了SSM+Swagger整合的demo,一顿瞎整,终于可以运行了。
不容易,因此分享这篇博客,祝新手朋友们早日脱坑!
注意:如果存在token验证或拦截的请注释掉相应的方法,Swagger才能正常使用。
一、pom.xml导入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>
选择:如遇到jdk1.5不能编译请参考下面,这个只是极少数情况,比如你的项目是拷来的,没有这种问题请忽略。
二、编写Swagger类
package com.shuyu.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
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 lanhaifeng
* @date 2018年08月13日 下午14: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;
}
}
三、controller类注解。其实只需要关注:@Api和@ApiOperation
package com.shuyu.controller;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.shuyu.service.TestService;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
@Controller
@RequestMapping(value="/test")
@Api(value="test",description="测试接口描述")
public class TestController {
@Autowired
private HttpServletRequest request;
@Autowired
private TestService service;
/**
* @Title: getHeader @Description: TODO @param @return @return String @throws
*/
@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;
}
@RequestMapping(value="/sql")
@ResponseBody
@ApiOperation(value="数据库查询",httpMethod="GET",notes="mysql数据库查询操作")
public List<Map<String, String>> getSql(@RequestParam(value="name",required=true)String name) {
System.out.println(getToken());
return service.query(name);
}
// get user agent
private String getUserAgent() {
return request.getHeader("user-agent");
}
// get request headers
private Map<String, String> getHeadersInfo() {
Map<String, String> map = new HashMap<String, String>();
Enumeration headerNames = request.getHeaderNames();
while (headerNames.hasMoreElements()) {
String key = (String) headerNames.nextElement();
String value = request.getHeader(key);
map.put(key, value);
}
return map;
}
// get request headers
private String getToken() {
Enumeration headerNames = request.getHeaderNames();
while (headerNames.hasMoreElements()) {
String key = (String) headerNames.nextElement();
if("token".equals(key)) {
String value = request.getHeader(key);
return value;
}
}
return null;
}
}
四、springmvc.xml配置加入
<!-- 4:扫描web相关的bean -->
<context:component-scan base-package="com.shuyu.controller" />
<!--整合 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/" />
五、在webapp文件下创建swagger文件夹,下载swagger-ui解压,把里面的dist文件复制到swagger文件下
下载地址https://github.com/swagger-api/swagger-ui
注意下图:index.html中的url="http://localhost:8080/api-docs",如果你在tomcat配置时加入了项目名称请在8080后面加入项目名称
六、汉化成中文,在index.html中引入汉化的两个js文件
<script src="lang/translator.js" type='text/javascript'></script>
<script src="lang/zh-cn.js" type='text/javascript'></script>
七、访问:输入http://localhost:8080/swagger/dist/index.html
注:我的项目在tomcat配置时是没有加项目名的,因此访问路径是http://localhost:8080/swagger/dist/index.html