给项目添加swagger

1.什么是swagger

官方介绍Swagger是一个规范且完整的框架,提供描述、生产、消费和可视化RESTful Web Service。
专业角度:Swagger是由庞大工具集合支撑的形式化规范。这个集合涵盖了从终端用户接口、底层代码库到商业API管理的方方面面。

2.为什么要用swagger

作为一个很懒的码代码的猿呢,对于一些API的理解总是很模糊不清,但是,总想着能直接验证一下自己的理解就好了,而不是需要去项目写测试代码来验证自己的想法。所以说,API文档应该有直接运行的能力。而Swagger就是这样的一个东西,它可以为已有项目的生成具备执行能力的样式化API文档,这样可以极大的方便程序员对前端后台进行对接整合。

3.开发环境:

JDK:1.8

swagger:2.6.0

gradle依赖:

compile(
"io.springfox:springfox-swagger2:2.6.1",
"io.springfox:springfox-swagger-ui:2.6.1"
)

4.swagger配置类

通过注解将配置了注入spring

 1 package com.zhoulei.config;
 2 
 3 
 4 import org.springframework.context.annotation.Bean;
 5 import org.springframework.context.annotation.Configuration;
 6 import org.springframework.web.servlet.config.annotation.EnableWebMvc;
 7 import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
 8 import springfox.documentation.builders.ApiInfoBuilder;
 9 import springfox.documentation.builders.PathSelectors;
10 import springfox.documentation.builders.RequestHandlerSelectors;
11 import springfox.documentation.service.ApiInfo;
12 import springfox.documentation.service.Contact;
13 import springfox.documentation.spi.DocumentationType;
14 import springfox.documentation.spring.web.plugins.Docket;
15 import springfox.documentation.swagger2.annotations.EnableSwagger2;
16 
17 /**
18  * @author: lzhou
19  * @date: 2018/09/07 14:09
20  * @version: V1.0
21  */
22 @Configuration
23 //@EnableWebMvc
24 @EnableSwagger2
25 public class SwaggerConfig{
26     @Bean
27     public Docket createRestApi() {
28         return new Docket(DocumentationType.SWAGGER_2)
29                 .apiInfo(apiInfo())
30                 .select()
31                 .apis(RequestHandlerSelectors.basePackage("com.zhoulei.controller"))
32                 .paths(PathSelectors.any())
33                 .build();
34     }
35 
36     private ApiInfo apiInfo() {
37         return new ApiInfoBuilder()
38                 .title("Springboot测试项目")
39                 .description("如有调用问题请发送邮箱:384701202@qq.com")
40                 .contact(new Contact("EP-lzhou","http://www.baidu.com","384701202@qq.com"))
41                 .version("1.0")
42                 .build();
43     }
44 
45 }

5.编写control类进行测试

@ResponseBody
    @RequestMapping(value = "/test", method = RequestMethod.POST)
    @ApiOperation(value ="Springboot测试接口" ,notes = "Springboot测试接口")
    public String Test(@RequestBody String info){
        return info;
    }

6.运行项目,输入地址:http://localhost:9099/swagger-ui.html#!(自己的地址和端口)效果如下:

 

转载于:https://www.cnblogs.com/lei-zhou/p/9604610.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值