本文共 1980 字,大约阅读时间需要 6 分钟。
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@EnableSwagger2public 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