Spring Rest Docs json 请求和响应格式
Spring Rest Docs json request and response formatting
使用Preprocessors
prettyPrint()
得到的json如下:
{
"testId" : "message-1",
"testType" : "TYPE",
"nestedList" : [ {
"nestedTestId" : 5,
"nestedTestCode" : 2,
"anotherNestedList" : [ {
"anotherNestedFirst" : [ {
"anotherId" : 1,
"anotherValue" : "VALUE_1",
"anotherDescription" : null
}, {
"anotherId" : 2,
"anotherValue" : "VALUE_2",
"anotherDescription" : "DESCRIPTION"
} ]
} ]
} ]
}
我希望它看起来像这样:
braces are newline with slightly (2-space) indented lists as formatted by most online json formatter.
{
"testId": "message-1",
"testType": "TYPE",
"nestedList": [
{
"nestedTestId": 5,
"nestedTestCode": 2,
"anotherNestedList": [
{
"anotherNestedFirst": [
{
"anotherId": 1,
"anotherValue": "VALUE_1",
"anotherDescription": null
},
{
"anotherId": 2,
"anotherValue": "VALUE_2",
"anotherDescription": "DESCRIPTION"
}
]
}
]
}
]
}
尝试了 Preprocessors
replacePattern
和其他方法,但没有成功。
任何帮助,将不胜感激。谢谢!
Spring REST Docs 在其默认配置中使用 Jackson 来漂亮地打印 JSON 请求和响应。您可以通过自定义漂亮的打印行为编写自己的 ContentModifier
并创建使用它的预处理器来自定义此行为:
OperationPreprocessor prettyPrint = new ContentModifyingOperationPreprocessor((content, contentType) -> {
ObjectMapper objectMapper = new ObjectMapper();
PrettyPrinter prettyPrinter = new DefaultPrettyPrinter()
.withArrayIndenter(DefaultIndenter.SYSTEM_LINEFEED_INSTANCE);
try {
return objectMapper.writer(prettyPrinter).writeValueAsBytes(objectMapper.readTree(content));
}
catch (IOException ex) {
return content;
}
});
上面的关键是将数组缩进配置为使用换行符而不是默认的空格。
然后您可以在记录操作时使用 prettyPrint
:
mockMvc.perform(get("/example")).andExpect(status().isOk())
.andDo(document("example", preprocessRequest(prettyPrint), preprocessResponse(prettyPrint)));
使用Preprocessors
prettyPrint()
得到的json如下:
{
"testId" : "message-1",
"testType" : "TYPE",
"nestedList" : [ {
"nestedTestId" : 5,
"nestedTestCode" : 2,
"anotherNestedList" : [ {
"anotherNestedFirst" : [ {
"anotherId" : 1,
"anotherValue" : "VALUE_1",
"anotherDescription" : null
}, {
"anotherId" : 2,
"anotherValue" : "VALUE_2",
"anotherDescription" : "DESCRIPTION"
} ]
} ]
} ]
}
我希望它看起来像这样:
braces are newline with slightly (2-space) indented lists as formatted by most online json formatter.
{
"testId": "message-1",
"testType": "TYPE",
"nestedList": [
{
"nestedTestId": 5,
"nestedTestCode": 2,
"anotherNestedList": [
{
"anotherNestedFirst": [
{
"anotherId": 1,
"anotherValue": "VALUE_1",
"anotherDescription": null
},
{
"anotherId": 2,
"anotherValue": "VALUE_2",
"anotherDescription": "DESCRIPTION"
}
]
}
]
}
]
}
尝试了 Preprocessors
replacePattern
和其他方法,但没有成功。
任何帮助,将不胜感激。谢谢!
Spring REST Docs 在其默认配置中使用 Jackson 来漂亮地打印 JSON 请求和响应。您可以通过自定义漂亮的打印行为编写自己的 ContentModifier
并创建使用它的预处理器来自定义此行为:
OperationPreprocessor prettyPrint = new ContentModifyingOperationPreprocessor((content, contentType) -> {
ObjectMapper objectMapper = new ObjectMapper();
PrettyPrinter prettyPrinter = new DefaultPrettyPrinter()
.withArrayIndenter(DefaultIndenter.SYSTEM_LINEFEED_INSTANCE);
try {
return objectMapper.writer(prettyPrinter).writeValueAsBytes(objectMapper.readTree(content));
}
catch (IOException ex) {
return content;
}
});
上面的关键是将数组缩进配置为使用换行符而不是默认的空格。
然后您可以在记录操作时使用 prettyPrint
:
mockMvc.perform(get("/example")).andExpect(status().isOk())
.andDo(document("example", preprocessRequest(prettyPrint), preprocessResponse(prettyPrint)));