SpringBoot集成swagger
来源:互联网 发布:布尔玛和孙悟空 知乎 编辑:程序博客网 时间:2024/06/05 02:04
先来一个最终成功的界面图~,~
第一步:导入相关maven依赖包(这里是已经建好springboot后的步骤,如何建立springboot项目请查阅其他资料。)
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.2.2</version>
</dependency>
第二步:在springboot的启动类中加入swager启用的注解
第三步:建立swager的配置类,这里可以配置我们第一幅图中标题,作者等等信息,具体参照我类中的内容与页面进行对比。
package com.log.rest.swagger;
import static com.google.common.base.Predicates.or;
import static springfox.documentation.builders.PathSelectors.regex;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.async.DeferredResult;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* SwaggerConfig
*/
@Configuration
@EnableSwagger2
public class SwaggerConfig
{
@SuppressWarnings("unchecked")
@Bean
public static Docket testApi()
{
return new Docket(DocumentationType.SWAGGER_2).groupName("log").genericModelSubstitutes(DeferredResult.class)
.useDefaultResponseMessages(false).forCodeGeneration(true).pathMapping("/")// base,最终调用接口后会和paths拼接在一起
.select().paths(or(regex("/api/.*")))// 过滤的接口
.build().apiInfo(testApiInfo());
}
private static ApiInfo testApiInfo()
{
return new ApiInfoBuilder().title("《通用微服务日志管理系统》")// 大标题
.description("Doublecom 日志微服务中心 RestFul API")// 详细描述
.version("1.0")// 版本
.termsOfServiceUrl("NO terms of service").contact("hf")// 作者
.licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html").build();
}
}
把它放在你springboot扫描的包目录下即可
第四步:去官网下载swager的zip文件,然后解压后我们只要将dist复制放在resource下即可,这里我把dist文件夹改成了swager,可以不改,我只是为了好认它。。。
下载网址:https://github.com/swagger-api/swagger-ui点击打开链接
修改url为:http://localhost:8080/{projectname}/api-docs
第五步:修改controller类,进行参数配置等等
package com.log.rest.controller;
import java.io.Serializable;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.log.core.facade.LogFacade;
import com.log.core.page.PageResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
/**
* <p>
* Title: LogController.java
* </p>
* <p style="color:red">
* Description: 日志微服务接口
* </p>
*
* @author hf
* @created 2017年11月28日,下午3:02:39
* @modified [who date description]
* @check [who date description]
*/
@RestController
@RequestMapping("/api/log")
@Api("日志微服务相关API")
public class LogController implements Serializable
{
private static final long serialVersionUID = -1341315236679577599L;
private static final Logger logger = LoggerFactory.getLogger(LogController.class);
@Autowired
private LogFacade logFacade;
@ApiOperation(value="插入日志信息",notes="按要求输入参数,插入日志数据")
@ApiImplicitParams({
@ApiImplicitParam(paramType="query",name="appName",dataType="String",required=true,value="企业名称",defaultValue="doublecom"),
@ApiImplicitParam(paramType="query",name="logType",dataType="String",required=true,value="日志类型",defaultValue="登录日志")
} )
@ApiResponses({
@ApiResponse(code=400,message="请求参数没填好"),
@ApiResponse(code=404,message="请求路径没有或页面跳转路径不对")
} )
/**
* @Description: 插入日志信息
* @param appName 企业/应用唯一标识
* @param logType 日志类型
* @param attributes 客户自定义属性名称 List<Map>类型
*/
@RequestMapping(value = "/insertLog", method = RequestMethod.POST)
public String insertLog(@RequestBody Map<?, ?> map)
{
logger.info("插入日志");
return logFacade.insertLog(map);
}
/**
* @Description: 查询日志信息
* @param appName 企业/应用唯一标识
* @param pageNum 分页参数
* @param pageSize 分页参数
* @return page
* @throws Exception
*/
@RequestMapping(value = "/queryLogs", method = RequestMethod.POST)
public PageResponse queryLogs(@RequestBody Map<?, ?> map)
{
PageResponse page = logFacade.queryLogs(map);
logger.info("查询日志");
return page;
}
/**
* 属性查询
* @param appName 企业/应用唯一标识
* @param pageNum 分页参数
* @param pageSize 分页参数
* @return jsonStr
* @throws Exception
*/
@RequestMapping(value = "/queryProps", method = RequestMethod.POST)
public String queryCustomPropsInfo(@RequestBody Map<?, ?> map)
{
logger.info("查询属性");
String data = logFacade.queryCustomPropsInfo(map);
return data;
}
}
最后:运行项目→ http://localhost:8080/swagger-ui.html#/
详细内容如下: