新建spring boot項(xiàng)目题禀,并添加swagger相關(guān)依賴,完整pom如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>boot-swagger</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>boot-swagger</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--swagger相關(guān)依賴start-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.9.6</version>
</dependency>
<!--swagger相關(guān)依賴end-->
<!--spring-data相關(guān)依賴-->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
新建swagger配置類
package com.example.bootswagger.config;
import com.fasterxml.classmate.TypeResolver;
import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.data.domain.Pageable;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.AlternateTypeRule;
import springfox.documentation.schema.AlternateTypeRuleConvention;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* @author liujy
* @description swagger配置類
* @since 2021-01-18 09:54
*/
@EnableSwagger2
@EnableSwaggerBootstrapUI
@Configuration
class SwaggerBootstrapUIConfiguration {
public static final String basePackage = "com.example.bootswagger";
public static final String title = "spring boot整合swagger";
public static final String des = "spring boot整合swagger";
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
// 設(shè)置包掃描路徑
.apis(RequestHandlerSelectors.basePackage(basePackage))
.paths(PathSelectors.any())
.build();
}
@Bean
public Docket createWebApi() {
// 創(chuàng)建 Docket 對象
Docket docket = new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage(basePackage))
.paths(PathSelectors.ant("/**/w/**"))
.build().groupName("Web端API接口文檔");
return docket;
}
@Bean
public Docket createMobileApi() {
// 創(chuàng)建 Docket 對象
Docket docket = new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage(basePackage))
.paths(PathSelectors.ant("/**/m/**"))
.build().groupName("Mobile端API接口文檔");
return docket;
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title(title)
.description(des)
.version("1.0")
.build();
}
/**
* 將Pageable轉(zhuǎn)換展示在swagger中
*/
@Bean
public AlternateTypeRuleConvention pageableConvention(final TypeResolver resolver) {
return new AlternateTypeRuleConvention() {
@Override
public int getOrder() {
return Ordered.HIGHEST_PRECEDENCE;
}
@Override
public List<AlternateTypeRule> rules() {
List<AlternateTypeRule> list = new ArrayList<>();
list.add(new AlternateTypeRule(resolver.resolve(Pageable.class), resolver.resolve(Page.class)));
return list;
}
};
}
@ApiModel
@Getter
@Setter
private class Page {
@ApiModelProperty(value = "請求分頁頁碼 (0..N)", example = "0")
private Integer page = 0;
@ApiModelProperty(value = "每頁的記錄數(shù)", example = "10")
private Integer size = 10;
@ApiModelProperty(value = "排序格式: property(,asc|desc). 默認(rèn)是升序 asc. 支持多個(gè)排序條件", example = "createdDate,desc")
private List<String> sort;
}
}
controller測試類
package com.example.bootswagger.controller;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author liujy
* @description web測試接口
* @since 2021-01-18 10:05
*/
@RestController
// 匹配swagger配置類中**/w/**的path配置
@RequestMapping(value = "/web/w")
@Api(tags = {"測試模塊-測試控制器"})
public class TestWebController {
@ApiOperation("這是一個(gè)web接口")
@GetMapping("/t1")
public String test(@PageableDefault(value = 10, sort = {"createDate"}, direction = Sort.Direction.DESC) final Pageable pageable) {
return "web interface success";
}
}
package com.example.bootswagger.controller;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author liujy
* @description mobile測試接口
* @since 2021-01-18 11:31
*/
@RestController
// 匹配swagger配置類中**/w/**的path配置
@RequestMapping(value = "/app/m")
@Api(tags = {"測試模塊-測試控制器"})
public class TestAppController {
@ApiOperation("這是一個(gè)mobile接口")
@GetMapping("/t1")
public String test() {
return "mobile interface success";
}
}
項(xiàng)目包結(jié)構(gòu)如圖:
啟動項(xiàng)目肮疗,訪問http://ip:port/doc.html或者http://ip:port/swagger-ui.html(注意:http://ip:port/swagger-ui.html可用來測試文件下載)
如圖丹锹,web測試接口中所需的page信息也已經(jīng)生效: