FreeOnGoo:
私は春のRESTドキュメントと私のAPIドキュメントを書きます。
コード例:
@Override
public void getById(String urlTemplate, PathParametersSnippet pathParametersSnippet, Object... urlVariables) throws Exception {
resultActions = mockMvc.perform(get(urlTemplate, urlVariables)
.principal(principal)
.contentType(APPLICATION_JSON))
.andExpect(status().isOk())
.andDo(print());
// do..
}
しかし、問題は、テストの結果が1行に答えていることです。そして、返されるデータの構造を理解することは非常に困難です。
応答例:
MockHttpServletResponse:
Status = 200
Error message = null
Headers = {Content-Type=[application/json;charset=UTF-8]}
Content type = application/json;charset=UTF-8
Body = {"creator":null,"modifier":null,"modificationTime":null,"creationTime":null,"id":100,"deleted":false,"name":"Name","description":null,"report":[{"creator":"System","modifier":"System","modificationTime":"2019-01-30T14:21:50","creationTime":"2019-01-30T14:21:50","id":1,"name":"Form name","reportType":{"creator":"System","modifier":"System","modificationTime":"2019-01-30T14:21:50","creationTime":"2019-01-30T14:21:50","id":1,"deleted":false,"name":"Raport"},"unmodifiable":true}]}
Forwarded URL = null
Redirected URL = null
Cookies = []
さらに、私が受け取った答えからもドキュメントフォーマットされていないJSONでドキュメントを生成します
何が私が間違っているのでしょうか?JSONのフォーマットを有効にする方法?
アンディ・ウィルキンソン:
あなたはきれいに印刷応答を生成するようにアプリケーションを構成する立場にない場合、あなたはRESTドキュメントがドキュメント化される前にそれらにあなたのためにそれを行うことができます。これはで説明されたカスタマイズの要求および応答のドキュメントのセクション:
前処理はして文書を呼び出すことによって構成された
OperationRequestPreprocessor
、および/またはOperationResponsePreprocessor
。インスタンスは、静的使用して得ることができるpreprocessRequest
とpreprocessResponse
のメソッドをPreprocessors
。例えば:this.mockMvc.perform(get("/")).andExpect(status().isOk()) .andDo(document("index", preprocessRequest(removeHeaders("Foo")), preprocessResponse(prettyPrint())));
リクエストこの場合に除去するために前処理されているFoo
ヘッダを、それがきれいに印刷表示されるように、応答は、前処理されています。