前言
技术栈:springboot、swagger2
gitee源码地址:https://gitee.com/shawsongyue/sevenhee_modules/tree/master/springboot_swagger
1.基础maven依赖
<!--Swagger-UI API文档生产工具-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
2.目录结构与配置文件
application.yml
server:
port: 8080
3.创建swagger配置类(Swagger2Config.class)
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.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* swagger2配置
*
* @author xiaosongyue
* @date 2021/04/16 11:09:04
*/
@Configuration
@EnableSwagger2
public class Swagger2Config {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
//api基础信息
.apiInfo(apiInfo())
//选择路径api
.select()
//设置为当前包路径
.apis(RequestHandlerSelectors.basePackage("com.xsy.sevenhee.controller")).paths(PathSelectors.any())
//构建
.build();
}
/**
* 构建Api文档的详细信息函数
*
* @return {@link ApiInfo}
*/
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
//页面标题
.title("spring Boot 测试使用 构建RestFul API")
//创建人消息
.contact(new Contact("xiaosongyue", "http://www.baidu.com", "1729497919@qq.com"))
//版本号
.version("1.0")
//描述
.description("api 描述")
//构建
.build();
}
}
4.创建controller层进行swagger文档注解编写(SwaggerController.class)
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
/**
* swagger控制器
*
* @author xiaosongyue
* @date 2021/04/16 11:26:39
*/
@RestController
@RequestMapping("/swagger")
@Api(value = "swagger模块",tags = "swaggerController")
public class SwaggerController {
@GetMapping("/add")
@ApiOperation(value = "增加",notes = "增加信息")
public String add(String username,String password){
System.out.println("账号:"+username);
System.out.println("密码:"+password);
return "增加成功";
}
@DeleteMapping("/delete")
@ApiOperation(value = "删除",notes = "删除信息")
public String delete(String id){
System.out.println("要删除的id:"+id);
return "删除成功";
}
@PostMapping("/update")
@ApiOperation(value = "修改",notes = "修改信息")
public String update(String id,String context){
System.out.println("要删除的id:"+id);
System.out.println("要删除的内容:"+context);
return "修改成功";
}
@GetMapping("/select")
@ApiOperation(value = "查询",notes = "查询信息")
public String select(){
System.out.println("开始查询");
return "查询成功";
}
}
4.创建主启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* swagger应用程序
*
* @author xiaosongyue
* @date 2021/04/16 11:08:27
*/
@SpringBootApplication
public class SwaggerApplication {
public static void main(String[] args) {
SpringApplication.run(SwaggerApplication.class,args);
}
}
5.启动访问接口
访问地址:http://localhost:8080/swagger-ui.html
6.git源码地址
访问地址:https://gitee.com/shawsongyue/sevenhee_modules/tree/master/springboot_swagger