SpringBoot-Swapper的使用

SpringBoot-Swapper的使用

1、导入maven依赖

2.x版本导入两个依赖

访问路径为:http://127.0.0.1:8080/swagger-ui.html

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>

3.x版本导入一个依赖

访问路径为:http://127.0.0.1:8080/swagger-ui/index.html

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-boot-starter</artifactId>
    <version>3.0.0</version>
</dependency>

2、创建配置类

config>SwapperConfig.java

@Configuration
@EnableSwagger2  //开启Swagger2
public class SwapperConfig {

    //配置Swapper的Docket的bean实例
    @Bean
    public Docket docket(Environment environment) {
//        获取到当前是生产环境还是发布环境,从而来进行自动的改变swapper配置
        Profiles dev = Profiles.of("dev");
        boolean flag = environment.acceptsProfiles(dev);
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(flag)
                .select()
//                RequestHandlerSelectors:
//                    basePackage 指定要扫描的包
//                    any() 扫描全部
//                    none() 不扫描
//                    withClassAnnotation 扫描类上的注解,参数是一个注解的放射对象,annocation.class
//                    withMethodAnnotation 扫描方法上的注解
                .apis(RequestHandlerSelectors.basePackage("com.tuzhi.controller"))
                .paths(PathSelectors.any())//过滤路径
                .build();
    }

//    配置Swapper信息
    public ApiInfo apiInfo() {
        Contact contact = new Contact("吕竟", "127.0.0.1/hello", "542918096@qq.com");
        return new ApiInfo(
                "兔子的Api",
                "这是一个HelloSwapper",
                "2.0",
                "urn:tos",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList<VendorExtension>()

        );
    }

}

3、注解

  1. @ApiModel(“xx实体类”):加在实体类上,给类注释

  2. @ApiModelProperty(“用户名xx”):加在属性上,给属性注释

  3. @ApiOperation(“xxx接口”):加在控制方法上,给方法注释

  4. @Api(tag = {“xxx接口”}):加在控制类上,给控制类注释

  5. @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "nameHello", value = "你要Hello的内容",dataType = "String")
    }) //加在控制类方法上,给方法都参数添加注释
    
  6. @ApiModel("用户")
    @Data
    public class User {
        @ApiModelProperty("用户名")
        private String username;
        @ApiModelProperty("密码")
        private String password;
    }
    
    @RestController
    @Api(tags = {"用户接口"})
    public class HelloController {
    
        @GetMapping("/hello/{nameHello}/{author}")
        @ApiOperation("hello")
    
        @ApiImplicitParams(value = {
                @ApiImplicitParam(name = "nameHello", value = "你要Hello的内容",dataType = "String"),
                @ApiImplicitParam(name = "author", value = "作者名",dataType = "int")
        })
        public String hello(String nameHello,int author) {
            return "hello" + nameHello + author;
        }
    
        @PostMapping("/add")
        @ApiOperation("添加用户")
        public User add(User user) {
    
            return user;
        }
    
    }
    
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值