SpringBoot---整合swagger2

一.步骤总结

1.在pom.xml中添加依赖

2.在主类中添加注解:@EnableSwagger2

3.通过浏览器访问:

4.自定义注解设置不需要生成接口文档的方法

二.步骤代码示例

1.在pom.xml中添加依赖

在项目的pom.xml中导入Spring-fox依赖。目前最新版本为2.9.2,所以导入的依赖也是这个版本。其中springfox-swagger2是核心内容的封装。springfox-swagger-ui是对swagger-ui的封装。

    <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.在主类中添加注解:@EnableSwagger2

添加此注解后表示对当前项目中全部控制器进行扫描

@SpringBootApplication
@EnableSwagger2
@MapperScan(basePackages = "com.yrp.mapper")
public class JspApplication {
    public static void main(String[] args) {
        SpringApplication.run(JspApplication.class, args);
    }

}

3.通过浏览器访问:

启动项目后在浏览器中输入http://ip:port/swagger-ui.html即可以访问到swagger-ui页面,在页面中可以可视化的进行操作项目中所有接口。

4.自定义注解设置不需要生成接口文档的方法

自定义贴在方法上的注解,并在SwaggerConfig中配置,在不需要生成接口文档的方法上面添加@NotIncludeSwagger注解后,该方法将不会被Swagger进行生成在接口文档中。

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface NotlncludeSwagger {
}


@Configuration
public class SwaggerConfig {

    @Bean
    public Docket getDocket() {

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(swaggerDemoApiInfo())
                .select()
                .apis(not(withMethodAnnotation(NotlncludeSwagger.class)))
                .build();

    }

    //配置ApiInfo信息
    private ApiInfo swaggerDemoApiInfo() {
        return new ApiInfoBuilder()
                .contact(new Contact("yrp", "http://www.baidu.com", "xxx163.com"))
                .title("这里是swageer标题")
                .description("这里是swagger的描述")
                .version("1.0.0")
                .build();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值