swagger作为一款不错的api查看工具在spring框架项目上使用十分广泛,使用界面如下图所示:

springboot开启swagger功能

具体使用方法:

1.添加以下依赖

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>x.y.z</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>x.y.z</version>
        </dependency>

2.创建Swagger配置类

@Configuration
@EnableSwagger2
public class Swagger2Config {
    
    @Bean
    public Docket createRestApi() {
            docket = new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apiInfo())
                    .select()
                    .apis(RequestHandlerSelectors.basePackage("com.goldmantis.controller"))
                    .paths(PathSelectors.any())
                    .build();
        return docket;
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("利用Swagger2构建RESTful APIs")
                .description("swagger demo")
                .termsOfServiceUrl("")
                .contact(new Contact("zhang san","",""))
                .version("1.0")
                .build();
    }

}

3.输入url访问,url拼接路径为:

http://ip:port/contextPath/swagger-ui.html,比如http://localhost:8080/swagger-ui.html

因为springboot默认可以使用/static/,/public/,/resources,/META-INF/resources目录下的静态资源文件,而swagger-ui插件提供的swagger-ui.html在/META-INF/resources下,如下图所示:

springboot开启swagger功能

相关文章:

  • 2021-07-23
  • 2021-09-14
  • 2021-07-24
  • 2021-06-27
  • 2022-01-04
  • 2022-12-23
  • 2021-11-02
猜你喜欢
  • 2021-12-29
  • 2021-06-27
  • 2021-11-29
  • 2021-05-27
  • 2021-07-02
  • 2021-07-04
  • 2022-12-23
相关资源
相似解决方案