如何禁用 Spring Rest Docs

问题描述

问题 我在我的项目中使用 Spring Rest Docs。它在开发阶段很有用,但我不希望它在生产中可供普通用户搜索引擎使用。

研究 我没有看到包含“nocache”和“noindex”等内容的机器人元标记。实际上预期的可能是一个 spring application.property 字段,如 spring.rest-docs.enabled=false。这样我就可以在生产 spring 配置文件中配置它。

问题如何禁用某些弹簧配置文件的休息文档。

解决方法

有三种方法可以做到:

  • (failed) 使用测试类注释阻止测试(无法将其设置为工作):@IfProfileValue(name = "spring.profiles.active",values = {"prod"})@EnabledIf("${spring.restdocs.enabled}")
  • 失败)配置 maven-resources-plugin 以使用 maven-profiles 和 spring 配置文件(看起来太麻烦):https://stackoverflow.com/a/45817386/1112963
  • (工作) 使用 /resources/static
  • 禁用 application-prod.properties 中的 spring.resources.add-mappings=false 文件夹访问
,

您可以将此属性添加到您的 application.properties

spring.test.restdocs.failOnUndocumentedFields=false
spring.test.restdocs.failOnUndocumentedParams=false

https://scacap.github.io/spring-auto-restdocs/#_configuration

MockMvc 设置期间可用的配置:

failOnUndocumentedParams - 如果构建应该在至少一个未记录的参数上失败 failOnUndocumentedFields - 如果构建应该在至少一个未记录的字段上失败