导入所需依赖

  1. <dependencies>
  2. <!-- 接口展示插件 -->
  3. <!-- swagger ui -->
  4. <dependency>
  5. <groupId>org.springdoc</groupId>
  6. <artifactId>springdoc-openapi-ui</artifactId>
  7. <version>${springdoc.version}</version>
  8. </dependency>
  9. <!-- 第三方接口ui展示插件,写此文章时尚未实现openapi3.0协议 -->
  10. <dependency>
  11. <groupId>com.github.xiaoymin</groupId>
  12. <artifactId>knife4j-spring-ui</artifactId>
  13. </dependency>
  14. </dependencies>
  15. <build>
  16. <plugins>
  17. <plugin>
  18. <groupId>org.springframework.boot</groupId>
  19. <artifactId>spring-boot-maven-plugin</artifactId>
  20. <configuration>
  21. <fork>true</fork>
  22. </configuration>
  23. <executions>
  24. <execution>
  25. <id>pre-integration-test</id>
  26. <goals>
  27. <goal>start</goal>
  28. </goals>
  29. </execution>
  30. <execution>
  31. <id>post-integration-test</id>
  32. <goals>
  33. <goal>stop</goal>
  34. </goals>
  35. </execution>
  36. </executions>
  37. </plugin>
  38. <plugin>
  39. <groupId>org.springdoc</groupId>
  40. <artifactId>springdoc-openapi-maven-plugin</artifactId>
  41. <version>0.2</version>
  42. <executions>
  43. <execution>
  44. <id>integration-test</id>
  45. <goals>
  46. <goal>generate</goal>
  47. </goals>
  48. </execution>
  49. </executions>
  50. </plugin>
  51. </plugins>
  52. </build>

application.yml 配置(可选)

  1. spring:
  2. resources:
  3. cache:
  4. period: 31536000
  5. cachecontrol:
  6. max-age: 31536000
  7. chain:
  8. html-application-cache: true
  9. enabled: true
  10. strategy:
  11. content:
  12. enabled: true
  13. paths: /webjars/swagger-ui/**.*

WebMvcConfigurer 配置

  1. /**
  2. *
  3. * 启动 springMvc自动配置即可,注意若是 extends WebMvcConfigurationSupport,需要自己配置
  4. * 不理解的话请 Google WebMvcConfigurationSupport 和 WebMvcConfigurer 的区别,建议 implements WebMvcConfigurer
  5. */
  6. @Configuration
  7. public class SystemWebMvcConfig implements WebMvcConfigurer {
  8. @Override
  9. public void addResourceHandlers(ResourceHandlerRegistry registry) {
  10. //上传文件配置
  11. // registry.addResourceHandler("/upload/**").addResourceLocations("file:///" + uploadFilePath);
  12. // spring boot 默认的资源位置配置
  13. registry.addResourceHandler("/**").addResourceLocations("classpath:/static/", "classpath:/public/",
  14. "classpath:/resources/", "classpath:/META-INF/resources/","/");// 静态资源路径
  15. // swagger3 配置
  16. registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
  17. registry.addResourceHandler("/**/swagger-ui/**").addResourceLocations("/webjars/");
  18. registry.addResourceHandler("/**/favicon.ico").addResourceLocations("classpath:/**/favicon.ico");
  19. // registry.addResourceHandler("/webjars/**").addResourceLocations("/webjars/").resourceChain(false)
  20. // .addResolver(new WebJarsResourceResolver()).addResolver(new PathResourceResolver());
  21. }
  22. }

统一返回数据格式配置

  1. import org.springframework.core.MethodParameter;
  2. import org.springframework.web.context.request.NativeWebRequest;
  3. import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
  4. import org.springframework.web.method.support.ModelAndViewContainer;
  5. public class HandlerMethodReturnValueHandlerProxy implements HandlerMethodReturnValueHandler {
  6. private static final int STATUS_CODE_SUCCEEDED = 200;
  7. //返回对象处理方法所在类的白名单
  8. private String[] whiteNames = new String[]{"org.springdoc."};
  9. private HandlerMethodReturnValueHandler proxyObject;
  10. public HandlerMethodReturnValueHandlerProxy(HandlerMethodReturnValueHandler proxyObject) {
  11. this.proxyObject = proxyObject;
  12. }
  13. @Override
  14. public boolean supportsReturnType(MethodParameter returnType) {
  15. return proxyObject.supportsReturnType(returnType);
  16. }
  17. @Override
  18. public void handleReturnValue(Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer,
  19. NativeWebRequest webRequest) throws Exception {
  20. boolean isProxy = true;
  21. for (String whiteName : whiteNames) {
  22. if (returnType.getDeclaringClass().getName().contains(whiteName)) {
  23. isProxy = false;
  24. break;
  25. }
  26. }
  27. if (isProxy) {
  28. R hs = new R(returnValue);
  29. proxyObject.handleReturnValue(hs, returnType, mavContainer, webRequest);
  30. } else {
  31. proxyObject.handleReturnValue(returnValue, returnType, mavContainer, webRequest);
  32. }
  33. }
  34. }

验证

打开浏览器 http://127.0.0.1/swagger-ui.html
image.png