springfox源码_spring-boot集成swagger( 提供封装的starter源码)

本文介绍了Swagger的用途,通过添加springfox-swagger2和springfox-swagger-ui依赖来配置Swagger。展示了如何在Spring Boot中设置Swagger2,并提供了Swagger注解的用法示例。最后提到了将这些公共配置封装成spring-boot-starter以便后续使用。
摘要由CSDN通过智能技术生成

swagger简介

Swagger™的目标是为REST APIs 定义一个标准的,与语言无关的接口,使人和计算机在看不到源码或者看不到文档或者不能通过网络流量检测的情况下能发现和理解各种服务的功能。当服务通过Swagger定义,消费者就能与远程的服务互动通过少量的实现逻辑。类似于低级编程接口,Swagger去掉了调用服务时的很多猜测。

浏览 Swagger-Spec 去了解更多关于Swagger 项目的信息,包括附加的支持其他语言的库。

pom.xml(添加依赖)

io.springfox

springfox-swagger2

2.2.2

io.springfox

springfox-swagger-ui

2.4.0

swagger配置文件

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.spi.DocumentationType;

import springfox.documentation.spring.web.plugins.Docket;

import springfox.documentation.swagger2.annotations.EnableSwagger2;

@EnableSwagger2

@Configuration

@EnableWebMvc

//需要扫描的接口包

public class Swagger2Config {

@Bean

public Docket api() {

return new Docket(DocumentationType.SWAGGER_2)

.select()

.apis(RequestHandlerSelectors.any())

.paths(PathSelectors.any())

.build();

}

// 一些接口文档信息的简介

private ApiInfo apiInfo() {

return new ApiInfoBuilder()

.title("Spring Boot中使用Swagger2构建RESTful APIs")

.description("spring Boot 中构建RESTful API")

.termsOfServiceUrl("")

.contact("caizi")

.version("1.0")

.build();

}

}

swagger 注解用法

1.对整个controller的描述注解

@Api(value = "信息", description = "管理信息的API")

2.对每一个接口的描述

@ApiOperation(value = "修改信息", notes = "信息对象,信息标签,信息id")

3.对每个参数进行描述

@ApiParam(required = true, name = "postData",value = "用户信息json数据") @RequestParam("tagData") String tagData

示例(在线测试时,需要注意参数的paramType参数的类型)

@Api(value = "CorpusController", description = "管理语料的方法")

public class CorpusController {

@Autowired

ICorpusService iCorpusService;

@GETMapping(value = "/getCorpusByUserName")

@ApiOperation(value = "getCorpusByUserName", notes = "获取语料")

@ApiImplicitParams({

@ApiImplicitParam(name = "userName", value = "用户账号", required = true, dataType = "STRING",, paramType = "query"),

@ApiImplicitParam(name = "corpusType", value = "语料分组类型", required = true, dataType = "STRING",, paramType = "query")

})

public String getCorpusByUserName(@RequestParam("userName") String userName,

@RequestParam("corpusType") String corpusType) {

String result = iCorpusService.getCorpusByUserName(userName, corpusType).toString();

return result;

}

}

使用

// 如果发布服务器可能路径还需要加上项目名

http://ip:port/swagger-ui.html

swagger成功访问页面

结语

当然这些公共的配置,都可以配置成spring-boot-starter这样方便后面的调用,同时也能更好的维护。

// swagger-spring-boot-starter 源码下载地址

github: https://github.com/zg091418/swagger2springbootstarter

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值