Java爱好者 - 专注于Java技术Java爱好者 - 专注于Java技术

SpringMVC swagger的说明、配置及使用

spring swagger2.6的说明、配置及使用,以及header的设置
直接这样设置即可。

package cc.javar.config;
import java.util.ArrayList;
import java.util.List;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
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.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
@EnableWebMvc
@ComponentScan("cc.javar.controller")
public class SwaggerConfig {
    @Bean
    public Docket buildDocket(){
    ParameterBuilder ticketPar = new ParameterBuilder();
     List<Parameter> pars = new ArrayList<Parameter>();
     //header中的ticket参数非必填,传空也可以
    pars.add(ticketPar.name("login")
            .description("login")
            .modelRef(new ModelRef("string"))
            .parameterType("header")
            .required(false).build());
    pars.add(ticketPar.name("token")
            .description("token")
            .modelRef(new ModelRef("string"))
            .parameterType("header")
            .required(false).build());
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(buildApiInf())
                .select()
                .apis(RequestHandlerSelectors
                .basePackage("cc.javar.controller"))//controller路径
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(pars);
    }
    private ApiInfo buildApiInf(){
        return new ApiInfoBuilder()
                .title("SpringMVC接口")
                .termsOfServiceUrl("https://www.javar.cc")
                .description("此API提供接口调用")
                .license("JAVA文档")
                .licenseUrl("https://www.javar.cc")
                .version("1.0").build();
    }
}
本原创文章未经允许不得转载 | 当前页面:Java爱好者 - 专注于Java技术 » SpringMVC swagger的说明、配置及使用

评论

文章评论已关闭!