Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。总体目标是使客户端和文件系统作为服务器以同样的速度来更新。
作用:接口的文档在线自动生成,功能测试(特别是前后端分离项目,为与前端同学合作提供了很大的方便)。
本篇内容主要记录swagger配置,页面为swagger-bootstrap-ui 页面。
配置
第一步:配置pom.xml
<!-- swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
</dependency>
<!-- 引入swagger-bootstrap-ui包 -->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.8.5</version>
</dependency>
第二步:使用注解来进行启动swagger,添加配置类SwaggerConfig
package cn.cnic.zhongtai.system.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
*
* Created by wdd on 2019/11/1
*
**/
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
//http://ip地址:端口/项目名/swagger-ui.html#/
ApiInfo apiInfo = new ApiInfoBuilder()
.title("项目名称") //网站标题
.description("项目名称swagger RESTful APIs......") //网站描述
.version("9.0") //版本
.contact(new Contact("王栋栋","https://blog.csdn.net/Xiaodongge521","wangdongdong0224@163.com")) //联系人
.license("The Apache License") //协议
.licenseUrl("http://www.baidu.com") //协议url
.build();
return new Docket(DocumentationType.SWAGGER_2) //swagger版本
.pathMapping("/")
.select()
//扫描那些controller
.apis(RequestHandlerSelectors.basePackage("cn.cnic.zhongtai.system.controller"))
.paths(PathSelectors.any())
.build()
.apiInfo(apiInfo);
}
}
第三步:配置Controller的注解和方法上的注解,最下方有注解的详细解释;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
*
* 整体模型控制类
* Created by wdd on 2019/10/16
*
**/
@RestController
@RequestMapping(value = "/genTable")
@Api(value="整体模型控制类",tags = "整体模型控制类",description = "整体模型控制类描述")
public class GenTableController {
@Resource
private GenTableService genTableService;
/**
* 模型总表信息
* @param page
* @param limit
* @return
*/
@RequestMapping(value = "/model_list" , method = RequestMethod.GET)
@ApiOperation(value="模型总表信息",notes="模型总表信息,分页查询",response=String.class)
@ApiImplicitParams({
@ApiImplicitParam(name = "page", value = "分页的起始页", required = true, dataType = "String"),
@ApiImplicitParam(name = "limit", value = "每页显示的数量", required = true, dataType = "String")
})
public String modellist( @RequestParam(value="page",defaultValue="1") String page, @RequestParam (value="limit",defaultValue="10") String limit) {
Map<String, Object> rtnMap = new HashMap<String, Object>();
rtnMap.put("code", 500);
//当前页
Integer currPage = Integer.parseInt(page.trim());
//每页的数量
Integer pageSize = Integer.parseInt(limit.trim());
PageHelper.startPage(currPage, pageSize, true);
List<GenTable> GenTableList = genTableService.selectGenTableList();
PageInfo<GenTable> pageInfo=new PageInfo<>(GenTableList);
if(!GenTableList.isEmpty()) {
rtnMap.put("msg", "success");
rtnMap.put("code", "0");
rtnMap.put("data", GenTableList);
rtnMap.put("count", pageInfo.getTotal());
}else {
rtnMap.put("msg", "error");
rtnMap.put("code", "222222");
}
return JsonUtils.toJsonNoException(rtnMap);
}
}
这样呢就已经配置好,下面是访问效果
1.访问 原生的swagger 页面 ,http://127.0.0.1:666/swagger-ui.html,可以看到如下效果
2.访问 swagger-bootstrap-ui 的页面 ,http://127.0.0.1:666/doc.html,可以看到如下效果
swagger注解的详细说明:
swagger2使用说明:
@Api:用在类上,说明该类的作用
@ApiOperation:用在方法上,说明方法的作用
@ApiIgnore:使用该注解忽略这个API
@ApiImplicitParams:用在方法上包含一组参数说明
@ApiImplicitParam:用在@ApiImplicitParams注解中,指定一个请求参数的各个方面
paramType:参数放在哪个地方
header-->请求参数的获取:@RequestHeader
query-->请求参数的获取:@RequestParam
path(用于restful接口)-->请求参数的获取:@PathVariable
body(不常用)
form(不常用)
name:参数名
dataType:参数类型
required:参数是否必须传
value:参数的意思
defaultValue:参数的默认值
@ApiResponses:用于表示一组响应
@ApiResponse:用在@ApiResponses中,一般用于表达一个错误的响应信息
code:数字,例如400
message:信息,例如"请求参数没填好"
response:抛出异常的类
@ApiModel:描述一个Model的信息(这种一般用在post创建的时候,使用@RequestBody这样的场景,请求参数无法使用@ApiImplicitParam注解进行描述的时候)
@ApiModelProperty:描述一个model的属性
好了,内容就这么多,如有问题欢迎留言讨论~