文档地址
ip:端口号/资源路径/doc.html
springcloud 项目整合swagger
common-utils 当中编写配置类
其余模块引入common-utils
package com.allen.ggkt.swagger;
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
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.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableKnife4j
@EnableSwagger2
public class Swagger2Config {
@Bean
public Docket webApiConfig() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("ggkt")
.apiInfo(webApiInfo())
.select()
//只显示api路径下的页面
//.paths(Predicates.and(PathSelectors.regex("/api/.*")))
.build();
}
private ApiInfo webApiInfo() {
return new ApiInfoBuilder()
.title("网站-API文档")
.description("本文档描述了网站微服务接口定义")
.version("1.0")
.contact(new Contact("AllenIverrui", "http://AllenIverrui.top", "3037256239@qq.com"))
.build();
}
}
相关依赖
swagger 版本 3.0.3 2.0.8 ,springboot 版本选择2.7 以下不然会有空指针异常
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>swagger</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>swagger</name>
<description>swagger</description>
<properties>
<java.version>1.8</java.version>
<swagger.version>3.0.3</swagger.version>
</properties>
<dependencies>
<!-- 热部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- swagger依赖-->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<!--在引用时请在maven中央仓库搜索最新版本号-->
<version>${swagger.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
Config 类
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket docket() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
// 控制层包名,会自动扫描包里的所有接口
.apis(RequestHandlerSelectors.basePackage("com.example.swagger.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
// 展示在主页的信息
return new ApiInfoBuilder()
// 文档标题
.title("swagger测试")
// 文档描述
.description("swagger测试示例文档")
// 你的联系方式
.contact(new Contact("AllenIverrui", "test", "3037256239@qq.com"))
// 文档版本
.version("1.0")
.build();
}
}
实体类注解
@Data
@AllArgsConstructor
@NoArgsConstructor
public class HelloDTO {
@ApiModelProperty("文本")
String text;
}
控制器注解
@RestController
@Api(tags = "heloo 控制模块")
@RequestMapping("/HelloController")
public class HelloController {
@ApiOperation("test1")
@GetMapping("/test1")
public HelloDTO Test1(){
return new HelloDTO("test123");
}
}
文件上传接口
@PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE, value = "/upload")
public Result uploadFile(@RequestPart @ApiParam(value = "上传的文件",required = true) MultipartFile file){
String url = FileService.upload(file);
return Result.ok(url).message("文件上传成功");
}
SpringCloud
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-micro-spring-boot-starter</artifactId>
<version>${knife4j.version}</version>
</dependency>