swagger2快速搭建《一》

  1. 添加Swagger2依赖
 <!-- swagger-mvc -->
          <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-core</artifactId>
            <version>1.5.8</version>
          </dependency>
          <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
             <version>2.5.0</version>
          </dependency>
          <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
             <version>2.5.0</version>
          </dependency>
          <dependency>
            <groupId>com.fasterxml</groupId>
            <artifactId>classmate</artifactId>
            <version>1.3.1</version>
          </dependency>
          <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.8.7</version>
            </dependency>
          <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.8.7</version>
          </dependency>
      <!-- swagger-mvc -->
  1. 创建Swagger2的配置类
@EnableWebMvc 
@EnableSwagger2 
/*@ComponentScan(basePackages = {"com.test"})  */
@Configuration 
public class RestApiConfig extends WebMvcConfigurationSupport{ 
 
    @Bean 
    public Docket createRestApi() { 
        return new Docket(DocumentationType.SWAGGER_2) 
                .apiInfo(apiInfo()) 
                .select() 
                .apis(RequestHandlerSelectors.basePackage("com.test")) 
                .paths(PathSelectors.any()) 
                .build(); 
    } 
 
    private ApiInfo apiInfo() { 
        return new ApiInfoBuilder() 
                .title("Spring 中使用Swagger2构建RESTful APIs") 
                .termsOfServiceUrl("http://blog.csdn.net/he90227") 
                .contact("郭敬仰") 
                .version("1.0") 
                .build(); 
    } 
}
  1. 添加对swagger2静态资源的访问
<mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/"/> 
<mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars
  1. 确保swagger2配置类被扫描到(假如RestApiConfig类在com.test目录下 )
<context:component-scan base-package="com.test" />

实际项目中注意事项:
(1)避免swagger2静态资源被拦截掉
这里写图片描述
(2)避免请求的链接被拦截
这里写图片描述
(3)哪个包需要支持swagger2必须指定
这里写图片描述
该demo的下载地址:https://gitee.com/jingyang3877/all-examples/blob/master/jingyang3877-swagger-test-demo-master.zip

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值