SpringBoot(八)整合swagger

整合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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值