Maven项目如何配置Swagger

1、导入swagger相关依赖:

为了避免http://localhost:8080/swagger-ui.html出现404错误,希望此处不要导入最新版本的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、配置swagger的config

@Configuration
@EnableSwagger2  //开启Swagger2
public class SwaggerConfig {
    
}

3、启动项目:界面介绍:

在这里插入图片描述

4、Swagger配置扫描接口:

Docket.select()属性介绍
public Docket defaultApi2() {
    Docket docket=new Docket(DocumentationType.SWAGGER_2)
            .apiInfo(apiInfo())
            .select()
            //RequestHandlerSelectors配置要扫描什么请求类型的接口
            //basePackage()指定要扫描的包
            //any()扫描全部
            //none()不扫描
            //withClassAnnotation()扫描类上的注解,参数是一个注解的反射对象
            //withMethodAnnotation()扫描方法上的注解
            .apis(RequestHandlerSelectors.basePackage("com.example.swagger.controller"))
            //paths()过滤什么路径
            .paths(PathSelectors.ant("/hello/**"))
            .build();
    return docket;
}

5、配置是否启用Swagger

public Docket defaultApi2() {
    Docket docket=new Docket(DocumentationType.SWAGGER_2)
            .apiInfo(apiInfo())
            .enable(true)//表示是否使用Swagger,如果为false,则Swagger不能再浏览器中访问
            .select()
            .apis(RequestHandlerSelectors.basePackage("com.example.swagger.controller"))
            .build();
    return docket;
}

6、Swagger如何生成静态接口文档:

方法一(使用springfox-swagger-ui依赖时):

​ 生成静态文档有两种方法
​ (1).编写java代码生成
​ (2).Maven插件完成
`注:具体操作见:https://blog.csdn.net/weixin_42648692/article/details/105641976

方法二(使用Knife4j依赖时):

(1)添加依赖坐标:

<dependency>
	<groupId>com.github.xiaoymin</groupId>
	<artifactId>knife4j-spring-boot-starter</artifactId>
	<version>2.0.7</version>
</dependency>

(2)修改配置文件:

@Configuration
@EnableSwagger2
class Swagger2Config {

    @Bean(value = "defaultApi2")
    //配置Swagger的Dock的bean实例
    public Docket defaultApi2() {
        Docket docket=new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo());
        return docket;
    }
//配置Swagger信息=apiInfo
    private ApiInfo apiInfo(){
        //作者信息
        Contact contact = new Contact("cxh", "http://localhost:8081", "邮箱");
        return new ApiInfo(
                "SwaggerApi文档",
                "swagger测试",
                "v1.0",//版本号
                "http://localhost:8081",
                contact,//作者信息
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());

    }

}

​ (3)重启服务查看接口

​ 访问地址:http://localhost:8081/doc.html,即可查看接口文档,并且提供word、Html和pdf等下载格式,实际运行截图见下图:

在这里插入图片描述
在这里插入图片描述
注:如果出现如下错误
在这里插入图片描述
请在配置文件中添加:

spring:
	  mvc:
    	pathmatch:
      		matching-strategy: ant_path_matcher
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值