SpringMVC swagger的说明、配置及使用

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

  1. package cc.javar.config;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.ComponentScan;
  6. import org.springframework.context.annotation.Configuration;
  7. import org.springframework.web.servlet.config.annotation.EnableWebMvc;
  8. import springfox.documentation.builders.ApiInfoBuilder;
  9. import springfox.documentation.builders.ParameterBuilder;
  10. import springfox.documentation.builders.PathSelectors;
  11. import springfox.documentation.builders.RequestHandlerSelectors;
  12. import springfox.documentation.schema.ModelRef;
  13. import springfox.documentation.service.ApiInfo;
  14. import springfox.documentation.service.Parameter;
  15. import springfox.documentation.spi.DocumentationType;
  16. import springfox.documentation.spring.web.plugins.Docket;
  17. import springfox.documentation.swagger2.annotations.EnableSwagger2;
  18. @Configuration
  19. @EnableSwagger2
  20. @EnableWebMvc
  21. @ComponentScan("cc.javar.controller")
  22. public class SwaggerConfig {
  23.     @Bean
  24.     public Docket buildDocket(){
  25.     ParameterBuilder ticketPar = new ParameterBuilder();
  26.      List<Parameter> pars = new ArrayList<Parameter>();
  27.      //header中的ticket参数非必填,传空也可以
  28.     pars.add(ticketPar.name("login")
  29.             .description("login")
  30.             .modelRef(new ModelRef("string"))
  31.             .parameterType("header")
  32.             .required(false).build());
  33.     pars.add(ticketPar.name("token")
  34.             .description("token")
  35.             .modelRef(new ModelRef("string"))
  36.             .parameterType("header")
  37.             .required(false).build());
  38.         return new Docket(DocumentationType.SWAGGER_2)
  39.                 .apiInfo(buildApiInf())
  40.                 .select()
  41.                 .apis(RequestHandlerSelectors
  42.                 .basePackage("cc.javar.controller"))//controller路径
  43.                 .paths(PathSelectors.any())
  44.                 .build()
  45.                 .globalOperationParameters(pars);
  46.     }
  47.     private ApiInfo buildApiInf(){
  48.         return new ApiInfoBuilder()
  49.                 .title("SpringMVC接口")
  50.                 .termsOfServiceUrl("https://www.javar.cc")
  51.                 .description("此API提供接口调用")
  52.                 .license("JAVA文档")
  53.                 .licenseUrl("https://www.javar.cc")
  54.                 .version("1.0").build();
  55.     }
  56. }
腾讯云服务器安全可靠高性能,多种配置供您选择
avatar

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: