swagger、springboot集成swagger

1、导入依赖

        <!--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、建包 (MVC思想) swagger用在controller层、swagger配置类

在这里插入图片描述

@Configuration
@EnableSwagger2 //开启功能
public class SwaggerConfig {
    //配置swagger的Docket的bean实例
    @Bean
    public Docket docket(Environment environment) {
        //显示要设置的swagger环境
        Profiles profiles=Profiles.of("dev","test");
        //通过environment 判断是否处在自己设定的环境下
        boolean flag = environment.acceptsProfiles(profiles);
        /**
         * .groupName("Thunder") 可以设置多个分组
         * .groupName("B")
         * .groupName("C")
         * */
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("Thunder")
                .apiInfo(apiInfo())
                //是否启用swagger
                .enable(true)
                .select()
                //指定扫描的包 com.thunder.controller
                //扫描所有 any()
                //none() 不扫描
                //withClassAnnotation:扫描类上注解
                //withMethodAnnotation:扫描类上注解
                .apis(RequestHandlerSelectors.basePackage("com.thunder.controller"))
                // paths 过滤什么路径
                //.paths(PathSelectors.ant("/thunder/**"))
                .build();
    }
    //配置基本信息
    private ApiInfo apiInfo() {
        Contact contact = new Contact("thunder",
                "http://www.baidu.com",
                "1657416567@qq.com");
        return new ApiInfo(
                "thunder的参考文档",
                "springboot入门精通",
                "V1.0",
                "http://www.baidu.com",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LINCENSE-2.0",
                new ArrayList<>()
        );
    }
}

3、实体类、控制器

@ApiModel("账户实体")
public class Account {
    @ApiModelProperty("账户")
    public String username;
    @ApiModelProperty("密码")
    public String password;
}

@Controller
@Api(tags = "测试请求类")
public class TestController {
    @RequestMapping("/test")
    @ResponseBody
    public String test(){
        return "success";
    }
    @GetMapping("/account")
    @ResponseBody
    @ApiOperation(value="获取用户信息",tags={"获取用户信息copy"},notes="注意问题点")
    public Account user(@ApiParam(name = "request",value = "请求参数request",required = false) HttpServletRequest request){
        System.out.println(request);
        return new Account();
    }
}

4、测试 地址栏输入:http://127.0.0.1:8080/swagger-ui.html
在这里插入图片描述

5、至此,swagger可以完成基本日常要求,如需要更多功能,可以寻找官方API,度娘。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值