springmvc+swagger2

笔者最近拿springmvc4.x+swagger 2.8(最新版本)集成过程中碰到一些问题,现在分享一下

1、引入的包
swagger2 的包 maven仓库上找的最新版

    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.8.0</version>
    </dependency>

    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.8.0</version>
    </dependency>

2、如果启动有报jackjson的错,请再引入jackjson的包

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.6.6</version>
    </dependency>

3、我看过很多版本的集成,有些是不引入swagger-ui包,那么你就需要去自己下载swagger-ui 然后把 dist目录下的文件全部拷贝到自己的静态资源下。
我们需要把swagger的访问配置下静态路径
<mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/"/>
<mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/"/>

4、项目里配置swagger

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @Author zhangwenfeng
 * @Date 2018/1/17
 * @Description
 */
@EnableWebMvc
@EnableSwagger2
@Configuration
public class RestApiConfig extends WebMvcConfigurationSupport {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("cn.zx.user.controller.api"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("用户中心api")
                .termsOfServiceUrl("www.baidu.com")
                .contact("xxxx")
                .version("1.1")
                .build();
    }
}

5、最后一个注意点,如果使用的是 fastjson 请把版本升级到1.2.15以上。

6、使用 swagger-ui包的 访问路径为 http://{ip}:{port}:{project}/swagger-ui.html

7、在被扫描的controller里 加swagger注解,具体注解使用方式自行百度.

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,155评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 47,153评论 6 342
  • 一、前言 Swagger是一款RESTFUL接口文档在线自动生成+功能测试的工具软件。Swagger是一个规范和完...
    JimmyGan阅读 8,325评论 2 7
  • # Swagger2 SpringMVC集成(非SpringBoot) Swgger 做过手机端接口的同学们都有过...
    zhaovov阅读 11,627评论 1 3
  • 媒体查询的书写模式@media 媒体类型 and (媒体特性){你的css样式}媒体类型:screen(屏幕)pr...
    WangYatao阅读 1,171评论 0 0

友情链接更多精彩内容