SwaggerConfig.java 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package com.qxgmat.util.swagger;
  2. /**
  3. * Created by GaoJie on 2017/10/31.
  4. */
  5. import com.google.common.base.Predicate;
  6. import org.springframework.context.annotation.Bean;
  7. import org.springframework.context.annotation.Configuration;
  8. import springfox.documentation.builders.ApiInfoBuilder;
  9. import springfox.documentation.builders.RequestHandlerSelectors;
  10. import springfox.documentation.service.ApiInfo;
  11. import springfox.documentation.spi.DocumentationType;
  12. import springfox.documentation.spring.web.plugins.Docket;
  13. import springfox.documentation.swagger2.annotations.EnableSwagger2;
  14. import javax.servlet.ServletContext;
  15. import static com.google.common.base.Predicates.or;
  16. import static springfox.documentation.builders.PathSelectors.regex;
  17. @Configuration
  18. @EnableSwagger2
  19. public class SwaggerConfig {
  20. @Bean
  21. public Docket createFrontendApi(ServletContext servletContext) {
  22. return new Docket(DocumentationType.SWAGGER_2)
  23. .groupName("1.frontend")
  24. .apiInfo(apiFrontendInfo())
  25. .select()
  26. .apis(RequestHandlerSelectors.basePackage("com.qxgmat.controller"))
  27. .paths(webPath())
  28. .build()
  29. .useDefaultResponseMessages(false)
  30. .forCodeGeneration(true);
  31. }
  32. @Bean
  33. public Docket createBackendApi() {
  34. return new Docket(DocumentationType.SWAGGER_2)
  35. .groupName("2.backend")
  36. .apiInfo(apiBackendInfo())
  37. .select()
  38. .apis(RequestHandlerSelectors.basePackage("com.qxgmat.controller"))
  39. .paths(adminPath())
  40. .build()
  41. .useDefaultResponseMessages(false)
  42. .forCodeGeneration(true);
  43. }
  44. private Predicate<String> adminPath() {
  45. return or(
  46. regex("/admin.*"),
  47. regex("/common.*")
  48. );
  49. }
  50. private Predicate<String> webPath() {
  51. // return new Predicate<String>() {
  52. // @Override
  53. // public boolean apply(String input) {
  54. // return !input.contains("admin");
  55. // }
  56. // };
  57. return or(
  58. regex("/api.*"),
  59. regex("/common.*")
  60. );
  61. }
  62. private ApiInfo apiFrontendInfo() {
  63. return new ApiInfoBuilder()
  64. .title("前端API")
  65. .termsOfServiceUrl("")
  66. .version("1.0")
  67. .build();
  68. }
  69. private ApiInfo apiBackendInfo() {
  70. return new ApiInfoBuilder()
  71. .title("管理端API")
  72. .termsOfServiceUrl("")
  73. .version("1.0")
  74. .build();
  75. }
  76. }