meven配置
<!-- swagger2--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.7.0</version> </dependency> <!-- swagger2-UI--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.7.0</version> </dependency>
springboot扫描
/** * Swagger2的接口配置*/ @Configuration @EnableSwagger2 public class SwaggerConfig { /** * 创建API */ @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) // 详细定制 .apiInfo(apiInfo()) .select() // 指定当前包路径 .apis(RequestHandlerSelectors.basePackage("com.xiao.tool")) // 扫描所有 .apis(RequestHandlerSelectors.any()) .paths(PathSelectors.any()) .build(); } /** * 添加摘要信息 */ private ApiInfo apiInfo() { // 用ApiInfoBuilder进行定制 return new ApiInfoBuilder() .title("标题:接口文档") .description("描述:接口文档") .contact(new Contact("tool", null, null)) .version("版本号: 1.0") .build(); } }
使用
@RestController @RequestMapping("/test") @Api(description = "test") public class TestController { @PostMapping("/test") @ApiOperation("test") public ApiResult test(Map map) throws Exception { return ApiResult.success("ok"); } }
主要注解
@Api(description = "test") 作用在Controller类上
@ApiOperation("test") 作用在方法上
@ApiModel 作用在实体类上
@ApiModelProperty 作用在实体属性上