Spring整合Swagger

朱雀 2020-10-18 12:12 1042阅读 0赞

api-swagger

启动Tomcat,访问:http://localhost:8090

实现步骤

1.添加Maven依赖

  1. <dependency>
  2. <groupId>io.springfox</groupId>
  3. <artifactId>springfox-swagger2</artifactId>
  4. <version>2.8.0</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>io.springfox</groupId>
  8. <artifactId>springfox-swagger-ui</artifactId>
  9. <version>2.8.0</version>
  10. </dependency>

2.下载swagger-ui
解压zip -> 复制dist文件 -> 重命名swagger-ui -> 放到webapp/static/plug目录下

3.自定义SwaggerConfig类

  1. /** * The Apache License 2.0 * Copyright (c) 2018 sep6th * @see http://springfox.github.io/springfox/docs/current/#configuring-springfox */
  2. @EnableSwagger2
  3. @Configuration
  4. public class SwaggerConfig{
  5. @Bean
  6. public Docket createRestApi() {
  7. return new Docket(DocumentationType.SWAGGER_2)
  8. .apiInfo(apiInfo())
  9. .produces(newHashSet(MediaType.APPLICATION_JSON_VALUE))
  10. .protocols(Sets.newHashSet("http"/* , "https" */))
  11. .forCodeGeneration(true)
  12. .select()
  13. .apis(RequestHandlerSelectors.any()) // 扫描所有路径下的api文档
  14. .paths(paths()) // 筛选路径,生成API文档
  15. .build();
  16. }
  17. /** * api基本信息 */
  18. private ApiInfo apiInfo() {
  19. return new ApiInfoBuilder()
  20. .title("API-Swagger项目 API文档 By sep6th") // 标题
  21. .description("API-Swagger项目有两个业务模块,学校管理和学生管理!") // 描述
  22. .license("Apache License 2.0")
  23. .version("1.1.0") // api 版本号
  24. .build();
  25. }
  26. /** * 筛选路径 */
  27. private Predicate<String> paths() {
  28. return Predicates.or(
  29. PathSelectors.regex("/student.*"),
  30. PathSelectors.regex("/school.*"));
  31. }
  32. }

4.MVC配置

  1. <!-- 配置扫描器, 使得@Controller注解生效 -->
  2. <context:component-scan base-package="com.sep6th.*.controller">
  3. <!-- 用swagger页面操作api时,不配置报406 -->
  4. <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
  5. </context:component-scan>
  6. <!-- 配置扫描器, 生成swagger-Api页面 -->
  7. <context:component-scan base-package="springfox.documentation.swagger2"/>
  8. <!-- 配置扫描器, 必须在MVC配置文件里,扫描自定义swagger配置 -->
  9. <context:component-scan base-package="com.sep6th.base.config"/>
  10. <mvc:annotation-driven />
  11. <!-- 处理静态资源被“/”所拦截的问题 -->
  12. <mvc:default-servlet-handler />
  13. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  14. <property name="prefix" value="/WEB-INF/view/"/>
  15. <property name="suffix" value=".jsp"/>
  16. </bean>

5.jsp页面

  1. <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  3. <html>
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  6. <title>API DOCS</title>
  7. <link href="https://fonts.googleapis.com/css?family=Open+Sans:400,700|Source+Code+Pro:300,600|Titillium+Web:400,600,700" rel="stylesheet">
  8. <link rel="stylesheet" type="text/css" href="static/plug/swagger-ui/swagger-ui.css" >
  9. <link rel="icon" type="image/png" href="static/plug/swagger-ui/favicon-32x32.png" />
  10. <link rel="icon" type="image/png" href="static/plug/swagger-ui/favicon-16x16.png" />
  11. <style> html { box-sizing: border-box; overflow: -moz-scrollbars-vertical; overflow-y: scroll; } *, *:before, *:after { box-sizing: inherit; } body { margin:0; background: #fafafa; } </style>
  12. </head>
  13. <body>
  14. <div id="swagger-ui"></div>
  15. </body>
  16. <script src="static/plug/swagger-ui/swagger-ui-bundle.js"> </script>
  17. <script src="static/plug/swagger-ui/swagger-ui-standalone-preset.js"> </script>
  18. <script> window.onload = function(){ const ui = SwaggerUIBundle({ url: "http://127.0.0.1:8090/v2/api-docs", dom_id: '#swagger-ui', deepLinking: true, presets: [ SwaggerUIBundle.presets.apis, SwaggerUIStandalonePreset ], plugins: [ SwaggerUIBundle.plugins.DownloadUrl ], layout: "StandaloneLayout" }) window.ui = ui; } </script>
  19. </html>

6.Controller

  1. @Controller
  2. public class ApiController {
  3. /** * 显示API 文档 */
  4. @RequestMapping("/api")
  5. public String api(){
  6. return "api-docs";
  7. }
  8. }

发表评论

表情:
评论列表 (有 0 条评论,1042人围观)

还没有评论,来说两句吧...

相关阅读