How Can I Generate Spring REST Docs Snippets from the Contracts?

当希望使用 Spring REST Docs 包含 API 的请求和响应时,如果您正在使用 MockMvc 和 RestAssuredMockMvc,您只需要对设置进行一些细微的更改。要执行此操作,请包含以下依赖项(如果您尚未这样做):

When you want to include the requests and responses of your API by using Spring REST Docs, you only need to make some minor changes to your setup if you are using MockMvc and RestAssuredMockMvc. To do so, include the following dependencies (if you have not already done so):

  • maven

  • gradle

Unresolved directive in contract-dsl-rest-docs.adoc - include::{standalone_restdocs_path}/http-server/pom.xml[]
Unresolved directive in contract-dsl-rest-docs.adoc - include::{standalone_restdocs_path}/http-server/build.gradle[]

接下来,您需要对基类进行一些更改。以下示例使用 WebAppContext 和 RestAssured 的独立选项:

Next, you need to make some changes to your base class. The following examples use WebAppContext and the standalone option with RestAssured:

  • WebAppContext

  • Standalone

Unresolved directive in contract-dsl-rest-docs.adoc - include::{standalone_restdocs_path}/http-server/src/test/java/com/example/fraud/FraudBaseWithWebAppSetup.java[]
Unresolved directive in contract-dsl-rest-docs.adoc - include::{standalone_restdocs_path}/http-server/src/test/java/com/example/fraud/FraudBaseWithStandaloneSetup.java[]

您无需指定为生成的片段设置的输出目录(从 Spring REST Docs 版本 1.2.0.RELEASE 开始)。

You need not specify the output directory for the generated snippets (since version 1.2.0.RELEASE of Spring REST Docs).