1.集成jar包
compile group: 'io.springfox', name: 'springfox-swagger2', version: '2.9.2' compile group: 'io.springfox', name: 'springfox-swagger-ui', version: '2.9.2'
2.新建config类
@Configuration @EnableSwagger2 public class SwaggerConfig { public Docket buildDocket(){ return new Docket(DocumentationType.SWAGGER_2) .apiInfo(buildApiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.example.demo")) .paths(PathSelectors.any()) .build(); } private ApiInfo buildApiInfo(){ return new ApiInfoBuilder() .title("api接口文档") .version("2.2.2") .description("框架基础接口文档") .contact(new Contact("test","www.baidu.com","he.zhao@chwang.ai")) .build(); } }
3.controller中使用注解
@RestController @RequestMapping(value = "/api/product") @Api(tags = "商品") public class ProductCtrl { @ApiOperation(httpMethod = "GET", value = "商品推荐", notes = "首页商品推荐") @RequestMapping(value = "/recommend") public String recommend() { return ""; } @ApiOperation(httpMethod = "POST", value = "商品详情", notes = "点击商品/图片查看商品详情") @RequestMapping(value = "/productInfo") public String productInfo() { return ""; } }
4.启动spring boot,访问 http://localhost:8080/swagger-ui.html