swagger+Spring MVC+gradle

一. build.gradle引入包

https://mvnrepository.com/artifact/io.springfox/springfox-swagger2
https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui(该包里面本身含有显示页面)

https://mvnrepository.com/artifact/com.github.xiaoymin/swagger-bootstrap-ui

二. 创建SwaggerConfig配置类

@Configuration
@EnableSwagger2
//@EnableWebMvc
public class SwaggerConfig {

    @Bean
    public Docket createRestApi(){
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build()
                .apiInfo(apiInfo());
    }
    private ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                .title("swagger")
                .description("swagger API")
                .termsOfServiceUrl("http://xxx.com")
                .license("")
                .licenseUrl("")
                .version("1.0.0")
                .build();
    }
}

三. WebConfig类添加静态资源访问路径

springfox-swagger-ui或swagger-bootstrap-ui包里面本身包含ui访问页面,所以不需要添加额外的ui包

  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry){
    //swagger-ui页面显示
    registry.addResourceHandler("/swagger-ui.html")
            .addResourceLocations("classpath:/META-INF/resources/swagger-ui.html");
    //doc.html页面显示
    registry.addResourceHandler("/doc.html")
            .addResourceLocations("classpath:/META-INF/resources/doc.html");
    registry.addResourceHandler("/webjars/**")
            .addResourceLocations("classpath:/META-INF/resources/webjars/");
  }

四. controller类插入swagger注解

五. 访问

页面访问:http://localhost:8080/swagger-ui.html#,或http://localhost:8080/doc.html

JSON数据访问:http://localhost:8080/v2/api-docs

目录

一. build.gradle引入包

二. 创建SwaggerConfig配置类

三. WebConfig类添加静态资源访问路径

四. controller类插入swagger注解

五. 访问


 

目录

一. build.gradle引入包

二. 创建SwaggerConfig配置类

三. WebConfig类添加静态资源访问路径

四. controller类插入swagger注解


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值