swagger2快速搭建《一》

  1. 添加Swagger2依赖
 
          
            io.swagger
            swagger-core
            1.5.8
          
          
            io.springfox
            springfox-swagger2
             2.5.0
          
          
            io.springfox
            springfox-swagger-ui
             2.5.0
          
          
            com.fasterxml
            classmate
            1.3.1
          
          
            com.fasterxml.jackson.core
            jackson-annotations
            2.8.7
            
          
            com.fasterxml.jackson.core
            jackson-core
            2.8.7
          
      
  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静态资源的访问
 
 
   
  • 确保swagger2配置类被扫描到(假如RestApiConfig类在com.test目录下 )
  • 
    

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

    你可能感兴趣的:(swagger2)