工具类代码
  1. package com.example.demo.config;
  2. import io.swagger.annotations.Api;
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5. import springfox.documentation.builders.ApiInfoBuilder;
  6. import springfox.documentation.builders.RequestHandlerSelectors;
  7. import springfox.documentation.service.ApiInfo;
  8. import springfox.documentation.service.Contact;
  9. import springfox.documentation.spi.DocumentationType;
  10. import springfox.documentation.spring.web.plugins.Docket;
  11. import springfox.documentation.swagger2.annotations.EnableSwagger2;
  12. /**
  13. * @author Feri
  14. * @date 2021/12/27 11:44
  15. * @description:TODO
  16. */
  17. @Configuration //标记这是配置类,beans
  18. @EnableSwagger2 //启用Swagger
  19. public class SwaggerConfig {
  20. /**
  21. * 构建文档的基本信息
  22. */
  23. public ApiInfo createApi(){
  24. return new ApiInfoBuilder().description("xxxx项目的接口在线文档,可以对接口进行测试等操作")
  25. .title("xxx项目的接口文档").contact(new Contact("Java2112","http://www.qfedu.com","xingfei_work@163.com"))
  26. .version("1.0.0").build();
  27. }
  28. /**
  29. * 就是ioc创建实例 修饰方法 方法必须返回对象
  30. */
  31. @Bean
  32. public Docket createDocket(){
  33. return new Docket(DocumentationType.SWAGGER_2).apiInfo(createApi())
  34. .select().apis(RequestHandlerSelectors.basePackage("com.example.demo.controller"))
  35. .build();
  36. }
  37. }

使用方法在浏览器输入doc.html