package com.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.web.context.request.async.DeferredResult;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * Created by tang.cheng on 2017/1/16.
 */
@Profile("dev")
@Configuration
@EnableSwagger2
public class Swagger2Configuration {

    /**
     * ApiImplicitParam 与 ApiParam 的区别
     * ApiImplicitParam: This is the only way to define parameters when using Servlets or other non-JAX-RS environments.
     * <p>
     * 对Servlets或者非 JAX-RS的环境，只能使用 ApiImplicitParam。
     * 在使用上，ApiImplicitParam比ApiParam具有更少的代码侵入性，只要写在方法上就可以了，但是需要提供具体的属性才能配合swagger ui解析使用。
     * ApiParam只需要较少的属性，与swagger ui配合更好。
     * 传递复杂对象 By ModelAttribute
     * (1) ModelAttribute 是Spring mvc的注解，这里Swagger可以解析这个注解，获得User的属性描述--good
     * 原文链接：http://www.jianshu.com/p/b730b969b6a2
     * (2)传递复杂对象 By RequestBody
     * json格式传递对象使用RequestBody注解
     * (3) PathVariable是Spring 的注解，对于这种简单的参数，就可以不用写ApiParam来描述接口参数。
     * <p>
     * <p>
     * Every Docket bean is picked up by the swagger-mvc framework - allowing for multiple
     * swagger groups i.e. same code base multiple swagger resource listings.
     * http://springfox.github.io/springfox/docs/current/#introduction
     */
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)//Docket, Springfox’s, primary api configuration mechanism is initialized for swagger specification 2.0
                .groupName("SelectData")//分组，在html页面展示时有用。启用group选项会更改api json的访问路径。可以在/swagger-resources中location字段中找到
                .select()//select() returns an instance of ApiSelectorBuilder to give fine grained control over the endpoints exposed via swagger.
                .apis(RequestHandlerSelectors.basePackage("com.web"))
                .paths(PathSelectors.any())// .paths(Predicates.or(PathSelectors.regex("/api/.*")))//过滤的接口,此片过滤掉/api/打头的接口
                .build()//The selector requires to be built after configuring the api and path selectors. Out of the box we provide predicates for regex, ant, any, none
//                .ignoredParameterTypes(Student.class)//swagger-ui.html中如果有返回值是Student时，就会显示 Response Class (Status 200) OK <span class="strong">Student is not defined!</span>
                .genericModelSubstitutes(DeferredResult.class)//异步http请求
                .forCodeGeneration(true)//By default, types with generics will be labeled with '\u00ab'(<<), '\u00bb'(>>), and commas. This can be problematic with things like swagger-codegen. You can override this behavior by implementing your own GenericTypeNamingStrategy.
                .pathMapping("/")// 在这里可以设置请求的统一前缀；默认请求都是以 / 根路径开始，如果我们的应用不是部署在根路径，比如以/platform（应用名）部署，则可以通过一下方式设置请求的统一前缀。
                .apiInfo(apiInfo())
                .useDefaultResponseMessages(false)//使用默认的响应信息true：默认响应信息将会回到全局的响应信息中；false:不加到全局的响应信息中
                ;
    }


    /**
     * api描述信息
     *
     * @return
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("SelectData")
                .description("DailyNews")
                .version("0.1")//本次发布的版本
                .contact(new Contact("Tang.Cheng", "https://github.com/helloworldtang", "793059909@qq.com"))
                .build();
    }
}

@@ -307,16 +307,74 @@
 uilder;%0A
+import springfox.documentation.builders.ParameterBuilder;%0A
 import s
@@ -477,32 +477,80 @@
 ndlerSelectors;%0A
+import springfox.documentation.schema.ModelRef;%0A
 import springfox
@@ -810,16 +810,77 @@
 gger2;%0A%0A
+import static com.google.common.collect.Lists.newArrayList;%0A%0A
 /**%0A * C
@@ -2762,16 +2762,812 @@
 y, none%0A
+                .globalOperationParameters(%0A                        newArrayList(new ParameterBuilder()%0A                                .name(%22Authorization%22)%0A                                .description(%22token%22)%0A                                .modelRef(new ModelRef(%22string%22))%0A                                /**%0A                                 * paramType(%E5%8F%82%E6%95%B0%E6%94%BE%E5%9C%A8%E5%93%AA%E4%B8%AA%E5%9C%B0%E6%96%B9):%0A                                 header--%3E%E8%AF%B7%E6%B1%82%E5%8F%82%E6%95%B0%E7%9A%84%E8%8E%B7%E5%8F%96%EF%BC%9A@RequestHeader%0A                                 query--%3E%E8%AF%B7%E6%B1%82%E5%8F%82%E6%95%B0%E7%9A%84%E8%8E%B7%E5%8F%96%EF%BC%9A@RequestParam%0A                                 path%EF%BC%88%E7%94%A8%E4%BA%8Erestful%E6%8E%A5%E5%8F%A3%EF%BC%89--%3E%E8%AF%B7%E6%B1%82%E5%8F%82%E6%95%B0%E7%9A%84%E8%8E%B7%E5%8F%96%EF%BC%9A@PathVariable%0A                                 body%EF%BC%88%E4%B8%8D%E5%B8%B8%E7%94%A8%EF%BC%89%0A                                 form%EF%BC%88%E4%B8%8D%E5%B8%B8%E7%94%A8%EF%BC%89%0A                                 */%0A                                .parameterType(%22header%22)%0A                                .build()))%0A
 //      

add global operation parameter Authorization