swagger 使用笔记

package ccue.cciec.dispatch.server.config;

import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
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.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

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


@Configuration
@EnableSwagger2
public class SwaggerAutoConfiguration {

    @Bean
    public Docket api() {


        ParameterBuilder ticketPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<Parameter>();
        ticketPar.name("Authorization").description("登录校验")//name表示名称,description表示描述
                .modelRef(new ModelRef("string")).parameterType("header")
                .required(false).defaultValue("token").build();//required表示是否必填,defaultvalue表示默认值
        pars.add(ticketPar.build());//添加完此处一定要把下边的带***的也加上否则不生效

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
//            加载所有类注释 ApiOperation 注解注释的controlller
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
//            加载所有方法注释 RestController  的 controller
//              .apis(RequestHandlerSelectors.withMethodAnnotation(RestController.class))
//            加载指定包下所有controller
                .apis(RequestHandlerSelectors.basePackage("ccue.cciec.dispatch.server.controller"))
                .paths(PathSelectors.any()).build().useDefaultResponseMessages(false)
                .globalOperationParameters(pars);//************把消息头添加
    }

    /**
     * 获取swagger ApiInfo
     *
     * @return
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                //页面标题
                .title("调度接口API")
                //创建人
                .contact(new Contact("SongXiangdong", "http://www.baidu.com", "[email protected]"))
                //版本号
                .version("1.0")
                //描述
                .description("调度接口API")
                .build();
    }

}

你可能感兴趣的:(第三方工具,网站的使用)