SwaggerConfig

SpringFox

package com.alibaba.force.api.shared.swagger;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.service.Tag;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @author van.yzt
 * @date 2017/07/31
 */
@Configuration
@EnableSwagger2
@EnableWebMvc
public class SwaggerConfig {
    @Bean
    public Docket healthDocket() {
        return new Docket(DocumentationType.SWAGGER_2).groupName("Health").select()
            .apis(RequestHandlerSelectors.basePackage("com.alibaba.force.api.health"))
            .paths(PathSelectors.any()).build().tags(new Tag("System API", "This is for health."))
            .useDefaultResponseMessages(false)
            .apiInfo(apiInfo("System API", "This is Force system API"));
    }

    @Bean
    public Docket internalDocket() {
        return new Docket(DocumentationType.SWAGGER_2).groupName("Internal").select()
            .apis(RequestHandlerSelectors.basePackage("com.alibaba.force.api.internal"))
            .paths(PathSelectors.any()).build().tags(new Tag("System API", "This is for internal API."))
            .useDefaultResponseMessages(false)
            .globalOperationParameters(privateToken())
            .apiInfo(apiInfo("Internal API", "This is Force internal API"));
    }

    @Bean
    public Docket repositoryDocket() {
        return new Docket(DocumentationType.SWAGGER_2).groupName("Repository").select()
            .apis(RequestHandlerSelectors.basePackage("com.alibaba.force.api.repository"))
            .paths(PathSelectors.any()).build().tags(new Tag("Repository API", "This is for repository api."))
            .useDefaultResponseMessages(false)
            .globalOperationParameters(privateToken())
            .apiInfo(apiInfo("Repository API", "This is Force repository API"));
    }

    @Bean
    public Docket userDocket() {
        return new Docket(DocumentationType.SWAGGER_2).groupName("User").select()
            .apis(RequestHandlerSelectors.basePackage("com.alibaba.force.api.user"))
            .paths(PathSelectors.any()).build().tags(new Tag("User API", "This is for user api."))
            .useDefaultResponseMessages(false)
            .globalOperationParameters(privateToken())
            .apiInfo(apiInfo("User API", "This is Force user API"));
    }

    @Bean
    public Docket keyDocket() {
        return new Docket(DocumentationType.SWAGGER_2).groupName("Key").select()
            .apis(RequestHandlerSelectors.basePackage("com.alibaba.force.api.key"))
            .paths(PathSelectors.any()).build().tags(new Tag("Key API", "This is for key api."))
            .useDefaultResponseMessages(false)
            .globalOperationParameters(privateToken())
            .apiInfo(apiInfo("Key API", "This is Force key API"));
    }

    private List<Parameter> privateToken() {
        return new ArrayList<Parameter>() {{
            add(new ParameterBuilder()
                .name("private_token")
                .description("Private token for person")
                .modelRef(new ModelRef("String"))
                .parameterType("query")
                .required(true)
                .build());
        }};
    }

    private ApiInfo apiInfo(String title, String description) {
        return new ApiInfo(title, description, "v3", "", DEFAULT_CONTACT, null, null);
    }

    private static final Contact DEFAULT_CONTACT = new Contact("van.yzt",
        "https://lark.alipay.com/aone/code+voice/gt7niw", "van.yzt@alibaba-inc.com");
}