springboot与swagger

springboot项目中使用swagger,快速生成api


pom中引入

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

swagger配置

支持在配置文件中配置是否显示api,需要显示api的接口uri。(此处使用到了在springboot中读取配置文件信息的知识。springboot读取配置文件)

@EnableSwagger2
@Component
public class Swagger2 {

    @Autowired
    private BasicProperties basicProperties;

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .enable(basicProperties.getSwaggerShow())
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.hfy"))
//                .paths(PathSelectors.any())
                //.paths(PathSelectors.regex("/ab/*"))
                //.apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                //.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(path())
                .build();
    }

    private Predicate<String> path() {
        List<Predicate<String>> list = new ArrayList<>();
        for (String pattern : basicProperties.getSwaggerPatterns()) {
            list.add(PathSelectors.regex(pattern));
        }
        return Predicates.or(list);
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("APIs")
                .contact("hfy")
                .version("1.0")
                .build();
    }

}

待完成

暂时没做到:swagger中搜索不可用。

参考

Spring Boot中使用Swagger2构建强大的RESTful API文档
SpringBoot项目生成RESTfull API的文档

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值