问题描述
我想用 spring rest 文档创建一个自定义的 HttRequestSnippet。
因为我希望 url 显示为 /accounts/{id} 而不是 /accounts/1 ,所以我必须创建一个自定义片段类。
我尝试了两种方法:
-
我试图让这一切正常进行。因此,我将 uri 作为参数传递给类。
public class CustomHttpRequestSnippet extends TemplatedSnippet { public CustomHttpRequestSnippet(String uri) { super("custom",Collections.singletonMap("path",uri)); } @Override protected Map<String,Object> createModel(Operation operation) { Map<String,Object> model = new HashMap<>(); model.put("custom-path",removeQueryStringIfPresent(extractUrlTemplate(operation))); return model; }
}
在我的测试中我做到了:
CustomHttpRequestSnippet customHttpRequestSnippet = new CustomHttpRequestSnippet("/accounts/{userId}");
并将其包含到文档中(..
.andDo(document("{methodName}",preprocessRequest(prettyPrint()),preprocessResponse(prettyPrint()),pathParameters(
parameterWithName("userId").description("userId of the requested user.")
),customHttpRequestSnippet,
这会导致错误:
org.springframework.restdocs.mustache.MustacheException$Context: No method or field with name 'method' on line 3
at org.springframework.restdocs.mustache.Template.checkForMissing(Template.java:270)
at org.springframework.restdocs.mustache.Template.getValue(Template.java:178)
at org.springframework.restdocs.mustache.Template.getValueOrDefault(Template.java:223)
at org.springframework.restdocs.mustache.Mustache$VariableSegment.execute(Mustache.java:787)
at org.springframework.restdocs.mustache.Template.executeSegs(Template.java:114)
at org.springframework.restdocs.mustache.Template.execute(Template.java:91)
at org.springframework.restdocs.mustache.Template.execute(Template.java:82)
at org.springframework.restdocs.templates.mustache.MustacheTemplate.render(MustacheTemplate.java:62)
at org.springframework.restdocs.snippet.TemplatedSnippet.document(TemplatedSnippet.java:82)
at org.springframework.restdocs.generate.RestDocumentationGenerator.handle(RestDocumentationGenerator.java:191)
at org.springframework.restdocs.mockmvc.RestDocumentationResultHandler.handle(RestDocumentationResultHandler.java:52)
at org.springframework.test.web.servlet.mockmvc$1.andDo(mockmvc.java:201)
此外,createModel
永远不会被调用。总的来说,这对我来说是一个快速解决方案。我更愿意将自定义方法视为标准 HttpRequestSnippet 类。我的意思是我希望能够使用标准请求 ( OperationRequest ) 并为其添加一些附加信息。因此,我会编写一个自定义 .snippet 来记录我需要的部分。
我尝试的第二个方法是扩展 HttpRequestSnippet.class 并使用 PathParameteRSSnippet 类的方法,因为这里的路径按照我的需要进行处理。
public class CustomHttpRequest extends HttpRequestSnippet {
public CustomHttpRequest(Map<String,Object> attributes) {
super(attributes);
}
@Override
protected Map<String,Object> createModel(Operation operation) {
Map<String,Object> model = super.createModel(operation);
model.put("custom-path",removeQueryStringIfPresent(extractUrlTemplate(operation)));
return model;
}
private String removeQueryStringIfPresent(String urlTemplate) {
int index = urlTemplate.indexOf('?');
if (index == -1) {
return urlTemplate;
}
return urlTemplate.substring(0,index);
}
private String extractUrlTemplate(Operation operation) {
String urlTemplate = (String) operation.getAttributes()
.get(RestDocumentationGenerator.ATTRIBUTE_NAME_URL_TEMPLATE);
Assert.notNull(urlTemplate,"urlTemplate not found. If you are using mockmvc did "
+ "you use RestDocumentationRequestBuilders to build the request?");
return urlTemplate;
}
}
这不会导致任何错误,但永远不会调用构造函数和 createModel 函数。
据我所知,我必须从文档中触发它(......我测试中的部分,但我不知道如何。似乎标准 HttpRequestSnippet 类的触发器是在其余部分完成的docs 代码。另外,.adoc 文件是自动生成的,还是我必须提供附加参数?
有人能告诉我我需要改变什么才能完成这项工作,还是我的方法总体上是错误的?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)