swagger使用
- Springboot项目
- 版本2.7.0
- pom文件引入
<!--swagger2 集成--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.7.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.7.0</version> </dependency>
- 添加java配置
@EnableSwagger2 @Configuration public class Swagger2Config { /** * Swagger2参数详解 * * @Api:修饰整个类,描述Controller的作用 * @ApiOperation:描述一个类的一个方法,或者说一个接口 * @ApiParam:单个参数描述@ApiModel:用对象来接收参数 * @ApiProperty:用对象接收参数时,描述对象的一个字段 * @ApiResponse:HTTP响应其中1个描述 * @ApiResponses:HTTP响应整体描述 * @ApiIgnore:使用该注解忽略这个API * @ApiError:发生错误返回的信息 * @ApiImplicitParam:一个请求参数 * @ApiImplicitParams:多个请求参数 */ @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() //为当前包路径 .apis(RequestHandlerSelectors.basePackage("com.hege.demo.controller")) .paths(PathSelectors.any()) .build(); } //构建 api文档的详细信息函数 private ApiInfo apiInfo() { return new ApiInfoBuilder() //页面标题 .title("demo服务接口开发文档") //创建人 .contact(new Contact("Maple", "", "1150640979@qq.com")) //版本号 .version("1.0") //描述 .description("demo服务接口开发文档") .build(); } }
- Controller使用
@RestController @RequestMapping("/coreCategory") @Api(tags = "基础数据管理") public class CoreCategoryController { @Autowired private ICoreCategoryService coreCategoryService; @ApiOperation(value = "查看基础数据") @GetMapping("/list") public R getList(Page<CoreCategory> page, CoreCategory coreCategory) { Page<CoreCategory> pageData = coreCategoryService.getList(page, coreCategory); return R.ok(pageData); } }
请求地址:http://127.0.0.1:8088/swagger-ui.html
- Bean使用
@Data @TableName("core_category") @ApiModel(value = "CoreCategory对象", description = "基础数据-系统基础数据表") public class CoreCategory { @ApiModelProperty(value = "主键id") @TableId(value = "id", type = IdType.UUID) private String id; @ApiModelProperty(value = "父级id(仅多级显示的时候使用)") @TableField("parent_id") private String parentId; }
swagger2汉化
- SpringBoot项目
- 版本:2.7.0
- 在resourece目录下创建\META-INF\resourece目录,然后创建一个名称为"swagger-ui.html" 的HTML文件
- 将以下内容放置html内部
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Swagger UI</title> <link rel="icon" type="image/png" href="webjars/springfox-swagger-ui/images/favicon-32x32.png" sizes="32x32"/> <link rel="icon" type="image/png" href="webjars/springfox-swagger-ui/images/favicon-16x16.png" sizes="16x16"/> <link href='webjars/springfox-swagger-ui/css/typography.css' media='screen' rel='stylesheet' type='text/css'/> <link href='webjars/springfox-swagger-ui/css/reset.css' media='screen' rel='stylesheet' type='text/css'/> <link href='webjars/springfox-swagger-ui/css/screen.css' media='screen' rel='stylesheet' type='text/css'/> <link href='webjars/springfox-swagger-ui/css/reset.css' media='print' rel='stylesheet' type='text/css'/> <link href='webjars/springfox-swagger-ui/css/print.css' media='print' rel='stylesheet' type='text/css'/> <script src='webjars/springfox-swagger-ui/lib/object-assign-pollyfill.js' type='text/javascript'></script> <script src='webjars/springfox-swagger-ui/lib/jquery-1.8.0.min.js' type='text/javascript'></script> <script src='webjars/springfox-swagger-ui/lib/jquery.slideto.min.js' type='text/javascript'></script> <script src='webjars/springfox-swagger-ui/lib/jquery.wiggle.min.js' type='text/javascript'></script> <script src='webjars/springfox-swagger-ui/lib/jquery.ba-bbq.min.js' type='text/javascript'></script> <script src='webjars/springfox-swagger-ui/lib/handlebars-4.0.5.js' type='text/javascript'></script> <script src='webjars/springfox-swagger-ui/lib/lodash.min.js' type='text/javascript'></script> <script src='webjars/springfox-swagger-ui/lib/backbone-min.js' type='text/javascript'></script> <script src='webjars/springfox-swagger-ui/swagger-ui.min.js' type='text/javascript'></script> <script src='webjars/springfox-swagger-ui/lib/highlight.9.1.0.pack.js' type='text/javascript'></script> <script src='webjars/springfox-swagger-ui/lib/highlight.9.1.0.pack_extended.js' type='text/javascript'></script> <script src='webjars/springfox-swagger-ui/lib/jsoneditor.min.js' type='text/javascript'></script> <script src='webjars/springfox-swagger-ui/lib/marked.js' type='text/javascript'></script> <script src='webjars/springfox-swagger-ui/lib/swagger-oauth.js' type='text/javascript'></script> <script src='webjars/springfox-swagger-ui/springfox.js' type='text/javascript'></script> <!-- 加入国际化的js --> <script src="webjars/springfox-swagger-ui/lang/translator.js" type="text/javascript"></script> <script src="webjars/springfox-swagger-ui/lang/zh-cn.js" type="text/javascript"></script> </head> <body class="swagger-section"> <div id='header'> <div class="swagger-ui-wrap"> <a id="logo" href="http://swagger.io"><img class="logo__img" alt="swagger" height="30" width="30" src="webjars/springfox-swagger-ui/images/logo_small.png"/><span class="logo__title">swagger</span></a> <form id='api_selector'> <div class='input'> <select id="select_baseUrl" name="select_baseUrl"/> </div> <div class='input'><input placeholder="http://example.com/api" id="input_baseUrl" name="baseUrl" type="text"/></div> <div id='auth_container'></div> <div class='input'><a id="explore" class="header__btn" href="#" data-sw-translate>Explore</a></div> </form> </div> </div> <div id="message-bar" class="swagger-ui-wrap" data-sw-translate> </div> <div id="swagger-ui-container" class="swagger-ui-wrap"></div> </body> </html>