![](/img/trans.png)
[英]How to resolve URI parameters in string URL through java/spring code?
[英]How to document path parameters both in the uri and in the query string?
在 JUnit 5 測試中,我試圖用以下內容記錄我的 Spring 引導應用程序 API:
(...)
this.webTestClient = WebTestClient.bindToApplicationContext(applicationContext)
.configureClient()
.filter(documentationConfiguration(restDocumentation)
.operationPreprocessors()
.withResponseDefaults(prettyPrint()))
.build();
(...)
webTestClient
.post().uri("/tokens/{id}/random?size=16", TOKEN_TEST)
.exchange()
.expectStatus().isOk()
.expectBody(RandomResponse.class)
.consumeWith(entityExchangeResult -> assertThat(entityExchangeResult.getResponseBody().getRandomBytes()).hasSize(16))
.consumeWith(document(
"get_random_bytes_as_json",
pathParameters(
parameterWithName("id").description("Unique identifier of the token"),
parameterWithName("size").description("Size of the random byte array")
)));
不幸的是,它沒有按我的預期工作,我面臨以下異常:
org.springframework.restdocs.snippet.SnippetException: Path parameters with the following names were not found in the request: [size]
at org.springframework.restdocs.request.PathParametersSnippet.verificationFailed(PathParametersSnippet.java:149)
at org.springframework.restdocs.request.AbstractParametersSnippet.verifyParameterDescriptors(AbstractParametersSnippet.java:108)
at org.springframework.restdocs.request.AbstractParametersSnippet.createModel(AbstractParametersSnippet.java:74)
查看代碼 ( PathParametersSnippet
),在提取實際參數之前,查詢字符串似乎已從 URI 中刪除。 這解釋了錯誤消息。 有人可以幫助我構建正確的代碼並查看我做錯了什么嗎?
非常感謝您花時間閱讀我
埃里克
當您仔細閱讀文檔時,答案很簡單。 有兩種不同的方法可以解決這兩個用例:
RequestDocumentation#requestParameters(ParameterDescriptor...)
RequestDocumentation#pathParameters(ParameterDescriptor... descriptors)
可以這樣組合:
.consumeWith(document(
"get_random_bytes_as_json",
pathParameters(
parameterWithName("id").description("Unique identifier of the token")
),
requestParameters(
parameterWithName("size").description("Size of the random byte array")
))
)```
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.