SpringBoot集成swagger

先来一个最终成功的界面图~,~

第一步:导入相关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#/

详细内容如下:





  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值