1.首先是添加maven依赖:
<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查看