基于spring-boot使用Swagger构建restful api文档
2017-12-01 00:00
916 查看
1.首先是添加maven依赖:
2.编写swagger的配置类:
通过@Configuration注解,让Spring来加载该类配置;
通过@EnableSwagger2注解来启用Swagger2。
通过createRestApi函数创建Docket的Bean;
apiInfo()用来创建该Api的基本信息(展现在文档页面中);
select()函数返回一个ApiSelectorBuilder实例用来控制哪些接口暴露给Swagger来展现。
3.使用:
paramType:
query: @RequestParam对应的参数;
path:@PathVariable对应的参数,即url中参数;
body:@RequestBody对应的参数。
通过http://localhost:8080//swagger-ui.html进行api查看
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.6.1</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.6.1</version> </dependency>
2.编写swagger的配置类:
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.builders.RequestHandlerSelectors; 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; /** * Author: 李阳 * Date: 2017年12月1日 * Description: */ @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.kevin.swagger")) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("Swagger系统RESTful API文档") .description("Swagger系统API") .termsOfServiceUrl("") .contact(new Contact("kevin的文档", null, null)) .version("0.0.0") .build(); } }
通过@Configuration注解,让Spring来加载该类配置;
通过@EnableSwagger2注解来启用Swagger2。
通过createRestApi函数创建Docket的Bean;
apiInfo()用来创建该Api的基本信息(展现在文档页面中);
select()函数返回一个ApiSelectorBuilder实例用来控制哪些接口暴露给Swagger来展现。
3.使用:
import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; @Controller @RequestMapping(value = "swagger-demo") @Api(value = "Swagger信息", description = "Swagger信息服务", hidden = true) public class SwaggerDemoController { @ApiOperation(value = "根据主键获得Swagger信息", httpMethod = "GET", response = ResponseEntityBody.class) @ApiImplicitParam(name = "id", value = "Swaggerid", required = true, dataType = "long", paramType = "path") @RequestMapping(method = RequestMethod.GET, value = "/{id}") @ResponseBody public ResponseEntityBody loadSwaggerInfo(@PathVariable(name = "id", required = true) Long id) { …… } @ApiOperation(value = "查询Swagger信息", httpMethod = "GET", response = ResponseEntityBody.class) @ApiImplicitParams({ @ApiImplicitParam(name = "SwaggerName", value = "Swagger姓名", required = false, dataType = "String", paramType = "query"), @ApiImplicitParam(name = "Swaggerer", value = "Swagger号", required = false, dataType = "String", paramType = "query") }) @RequestMapping(method = RequestMethod.GET, value = "") @ResponseBody public ResponseEntityBody loadSwaggers( @RequestParam(name = "SwaggerName", required = false) String memberName, @RequestParam(name = "Swaggerer", required = false) String cardNumber ) { …… } @ApiOperation(value = "添加Swagger", httpMethod = "POST", response = ResponseEntityBody.class) @ApiImplicitParam(name = "view", value = "Swagger数据对象", required = true, dataType = "SwaggerView", paramType = "body") @RequestMapping(method = RequestMethod.POST, value = "") @ResponseBody public ResponseEntityBody addSwagger(@RequestBody SwaggerView view) { …… } }
paramType:
query: @RequestParam对应的参数;
path:@PathVariable对应的参数,即url中参数;
body:@RequestBody对应的参数。
通过http://localhost:8080//swagger-ui.html进行api查看
相关文章推荐
- 在spring-boot中使用swagger2来构建RESTful API文档
- Spring Boot 2.0.0.M7 中使用Swagger2构建RESTful API文档
- Springboot中使用Swagger2构建RESTful API文档
- 使用Spring Boot&Swagger快速构建REST API并生成优美的API文档
- SpringBoot开发详解(八) -- 使用Swagger2构建API文档
- SpringBoot + mybatis + Swagger快速构建REST API并生成优美的API文档
- 使用 SpringBoot + Swagger 生成接口 API 文档
- Spring Boot中使用Swagger2构建API文档
- 基于Nginx+Spring Boot+Swagger的api文档实践
- 在Spring中使用Springfox和swagger生成restful风格的API文档
- springboot集成swagger2构建RESTful API文档
- springboot中使用swagger2构建restful接口文档
- SpringBoot入门——使用Swagger构建Restful API文档
- springboot集成swagger2构建RESTful API文档
- SpringBoot&Swagger构建REST API并生成API文档
- Spring boot 中使用swagger-ui实现 restful-api
- 使用Swagger在SpringBoot项目中管理API文档(使用Oauth2)
- Spring Boot构建RESTful API与单元测试
- Spring Boot中使用Swagger2构建强大的RESTful API文档
- Spring Boot构建RESTful API与单元测试