Spring系列整合Swagger

作者: z七夜 | 来源:发表于2018-07-03 15:23 被阅读141次

    SpringMvc整合Swagger(spring版本在4.1.8以上,深深的痛)

    1.先搭建好springmvc的环境
    2.修改pom文件

        <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>
    

    3.也需要引入jackson的包
    4.定义一个swagger的配置类

    @EnableWebMvc
    @Configuration
    @EnableSwagger2
    public class SwaggerConfig {
    
        @Bean
        public Docket buildDocket() {
            return new Docket(DocumentationType.SWAGGER_2)
                    .select()
                    .apis(RequestHandlerSelectors.basePackage("jk.zmn.mvc.controller")) //要扫描的API(Controller)基础包
                    .paths(PathSelectors.any()) // and by paths
                    .build();
    
        }
    
        private ApiInfo apiInfo() {
            return new ApiInfoBuilder()
                    .title("接口列表 v1.1.0") // 任意,请稍微规范点
                    .description("接口测试") // 任意,请稍微规范点
                    .termsOfServiceUrl("http://localhost:8080/swagger-ui.html") // 将“url”换成自己的ip:port
                    .contact("laowu") // 无所谓(这里是作者的别称)
                    .version("1.1.0")
                    .build();
        }
    }
    

    5.修改springmvc的配置文件,放行一些静态资源

      <mvc:resources location="classpath:/META-INF/resources/" mapping="swagger-ui.html"/>
        <mvc:resources location="classpath:/META-INF/resources/webjars/" mapping="/webjars/**"/>
        <!--&lt;!&ndash; 添加扫描配置类 &ndash;&gt;-->
        <bean class="jk.zmn.mvc.config.SwaggerConfig" />
    

    6.新建controller,定义一些相关的文档

    @RestController
    @RequestMapping("test")
    @Api(value = "Test")
    public class TestController {
    
        @RequestMapping("index")
        @ApiOperation(value = "进入首页面")
        public String index(){
            return "index";
        }
    
    }
    

    7.访问http://ip:端口/项目名/swagger-ui.html即可

    SpringBoot整合swagger

    1.新建springboot项目
    2.引入swagger的jar包

    <!-- Swagger -->
            <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>
    

    3.定义swagger的配置类

    @Configuration
    @EnableSwagger2
    public class SwaggerConfig {
    
        @Bean
        public Docket buildDocket() {
            return new Docket(DocumentationType.SWAGGER_2)
                    .select()
                    .apis(RequestHandlerSelectors.basePackage("jk.zmn.swaggerspringboot.controller")) //要扫描的API(Controller)基础包
                    .paths(PathSelectors.any()) // and by paths
                    .build()
                    .apiInfo(buildApiInf());
        }
    
        private ApiInfo buildApiInf() {
            return new ApiInfoBuilder()
                    .title("Spring Boot中使用Swagger2 UI构建API文档")
                    .contact("test")
                    .version("1.0.0")
                    .build();
        }
    }
    

    4.自定义controller生成文档
    5.访问http://ip:端口/项目名/swagger-ui.html即可

    相关文章

      网友评论

        本文标题:Spring系列整合Swagger

        本文链接:https://www.haomeiwen.com/subject/ditjuftx.html