整合swagger
一、swagger是什么
Swagger 是一个用于生成、描述和调用 RESTful 接口的 Web 服务。必须使用派生注解请求(GetMapping、PostMapping、PutMapping、DeleteMapping)。
通俗的来讲,Swagger 就是将项目中所有(想要暴露的)接口(接口就是该方法的请求路径,不是interface)展现在页面上,并且可以进行接口调用和测试的服务,(调接口就是给后端发送对应的请求)
1、是一款让你更好的书写API文档的规范且完整框架。
2、提供描述、生产、消费和可视化RESTful Web Service。
3、是由庞大工具集合支撑的形式化规范。这个集合涵盖了从终端用户接口、底层代码库到商业API管理的方方面面
访问路径:http://127.0.0.1:8081/swagger-ui.html#/
二、springboot集成swagger
1、导入依赖
<!--springboot集成swagger-->
<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>
2、配置文件
@Configuration//标为为配置类,相当于spring.xml
@EnableSwagger2
public class SwagerrConfiguration {
private static ApiInfo DEFAULT = null;
@Bean
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2);
}
}
3、配置yml文件,解决 高版本SpringBoot整合Swagger 启动报错Failed to start bean ‘documentationPluginsBootstrapper‘ 问题
spring:
#解决 高版本SpringBoot整合Swagger 启动报错Failed to start bean ‘documentationPluginsBootstrapper‘ 问题
mvc:
pathmatch:
matching-strategy: ant_path_matcher
4、测试
@RestController
@RequestMapping("/test")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/user") //使用 @GetMapping这个派生注解,就是为了测试swagger
//测试swagger访问这个路径http://localhost:8080/swagger-ui.html
public List<User> test(Integer pageNum,Integer pageSize){
PageInfo<User> pageInfo = userService.selectAllName(pageNum, pageSize);
List<User> list = pageInfo.getList();
System.out.println("pageInfo:"+pageInfo);
return list;
}
@PostMapping("/testPost")
public String testPost(User user){
return user.getName();
}
@RequestMapping("view")
public String view(){
return "哈哈哈哈哈哈哈哈哈哈哈哈哈";
}
}
4、访问swagger
http://127.0.0.1:8081/swagger-ui.html#/
必须使用 @GetMapping("/user") 使用 @GetMapping这个派生注解请求(GetMapping、PostMapping、PutMapping、DeleteMapping),就是为了测试swagger