根据在代码中使用自定义的注解来生成接口文档
使用方法:
1) 添加jar包
<!--SpringBoot整合Swagger2生成接口文档--> <dependency> <groupId>com.spring4all</groupId> <artifactId>swagger-spring-boot-starter</artifactId> <version>1.7.0.RELEASE</version> </dependency>2)添加配置类,指定相关包com.micro.controller
package com.micro.config; import com.spring4all.swagger.SwaggerProperties; 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.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; @Configuration @EnableSwagger2 public class Swagger2Config { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .pathMapping("/") .select() .apis(RequestHandlerSelectors.basePackage("com.micro.controller")) .paths(PathSelectors.any()) .build().apiInfo(new ApiInfoBuilder() .title("商品服务接口文档") .description("商品服务接口文档,详细信息......") .version("1.0") .contact("23423423423@qq.com") .license("The Apache License") .licenseUrl("http://www.baidu.com") .build()); } }
3)用的类中添加注解
package com.micro.controller; import com.micro.mapper.ProductMapper; import com.micro.pojo.Product; import com.micro.service.ProductService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; import java.util.List; @RestController @RequestMapping("/product") @Api(value="商品controller",tags={"商品服务接口"}) public class ProductController { @Autowired ProductService productService; /** * 01-对外暴露的接口 * @return */ @ApiOperation(value="查询所有的商品",notes = "测试") @GetMapping("/findProducts") public List<Product> findProducts(){ return productService.findProducts(); } }
备注:
@Api:用在请求的类上,表示对类的说明
tags="说明该类的作用,可以在UI界面上看到的注解"
value="该参数没什么意义,在UI界面上也看到,所以不需要配置"
@ApiOperation:用在请求的方法上,说明方法的用途、作用
value="说明方法的用途、作用"
notes="方法的备注说明"