spring-boot集成Springfox-Swagger2

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring-boot集成Springfox-Swagger2相关的知识,希望对你有一定的参考价值。

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class Swagger2Config {

    @Bean
    public Docket restApiForSensors() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("sensors data")
                .forCodeGeneration(true)
                .select()
//                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
//                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.ant("/api/sensors/data/**"))
                .build()
                .useDefaultResponseMessages(false);
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("倍全SpringBoot Swagger2 RESTful API")
                .contact(new Contact("hjzgg", "", "2570230521@qq.com"))
                .description("静态token=xxx")
                .version("2.0")
                .build();
    }
}

 

@ApiOperation(value="城市昨日数据总览+城市七天核心数据走势", notes="根据城市和日期展示数据")
@ApiImplicitParams({
        @ApiImplicitParam(name = "city", value = "城市名称", required = true, paramType = "query"),
        @ApiImplicitParam(name = "date", value = "日期", required = true, paramType = "query"),
        @ApiImplicitParam(name = "token", value = "静态token", required = true, paramType = "query")
})
@RequestMapping(value = "index", method = RequestMethod.GET)

 

运行效果:访问 http://localhost:8081/sensors-data/swagger-ui.html

 

参考资料:

  spring-boot集成Springfox-Swagger2

  Spring - AntPathMatcher

  Spring Swagger2 一些注解说明

 

以上是关于spring-boot集成Springfox-Swagger2的主要内容,如果未能解决你的问题,请参考以下文章

spring-boot 集成 rabbitmq

spring-boot 集成 rabbitmq

使用放心的spring-boot集成测试

有没有办法正确集成两个(或更多)Spring-Boot 项目?

spring-boot学习之集成mybatis

Spring-boot 和 Keycloak 集成