1、引入swagger需要的java类库
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.6.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.6.0</version> </dependency> <dependency> <groupId>com.fasterxml</groupId> <artifactId>classmate</artifactId> <version>1.3.1</version> </dependency>
2、引入SwaggerConfig.java类,并配置
package com.coracle.positec.xweb.swagger; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.context.request.async.DeferredResult; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; import static com.google.common.base.Predicates.or; import static springfox.documentation.builders.PathSelectors.regex; /** * SwaggerConfig */ @Configuration @EnableSwagger2 public class SwaggerConfig { /** * 可以定义多个组,比如本类中定义把test和demo区分开了 (访问页面就可以看到效果了) * */ @Bean public Docket testApi() { return new Docket(DocumentationType.SWAGGER_2) .groupName("api_bsd") .genericModelSubstitutes(DeferredResult.class) .useDefaultResponseMessages(false) .forCodeGeneration(false) .pathMapping("/") .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.coracle.positec.xweb.controller")).build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("保时得项目RESTful API") .description("保时得项目中构建RESTful API") .termsOfServiceUrl("") .contact("huangbaidong") .version("1.0") .build(); } }
3、配置Controller接口注解
swagger类注释
@Api(value = "订单类",tags = "订单类测试接口")
swagger接口注释
@ApiOperation("订单列表")
swagger参数注释
@ApiParam("订单ID")
4、配置实体注解
swagger实体类注释
@ApiModel("订单实体类")
swagger实体类中隐藏属性
@ApiModelProperty(hidden = true)
swagger实体属性备注
@ApiModelProperty("订单编号")