一 基础整合步骤
1,创建一个springboot web应用 不做过多陈述.
2,配置pom 文件
<!-- 整合swagger2 api文档 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
3.配置专有bean
@Configuration
public class Swagger2 {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.hnwhy.cultureCloud.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("springboot利用swagger构建api文档")
.description("简单优雅的restfun风格")
.version("1.0")
.build();
}
}
4,启动类配置
5.control层 配置示例
6,启动项目后 访问地址
http://localhost:xxxx(端口)/swagger-ui.html
二 扩展部分
实现同一接口支持多种Content-type(application/json、application/x-www-form-urlencoded等) 方式 的swagger 测试访问 。
工具类如下:
public class RequestMapToUtil {
/**
* 获取 request 中用POST方式"Content-type"是 "text/plain"发送的 json数据
*
* @author: sgy
* @date: 2018年8月28日
* @param request
* @return
* @throws IOException