私はSpring 4.3.8.RELEASEを使用しています。私は特定の禁止されたエラーのためのエラーメッセージを設定したいと思います。私は私のコントローラにこれを持っています。 "response"は "javax.servlet.HttpServletResponse"タイプです。HttpServletResponseバッファにコンテンツを出力するにはどうすればよいですか?
response.setStatus(HttpServletResponse.SC_FORBIDDEN);
response.setContentLength(errorMsg.length());
byte[] buffer = new byte[10240];
final OutputStream output = response.getOutputStream();
output.write(buffer, 0, errorMsg.length());
output.flush();
しかし、コンテンツは、少なくとも私はユニットテストでそれを見ることができない、返さ取得していないように見える...
final MvcResult result = mockMvc.perform(get(contextPath + "/myurl")
.contextPath(contextPath)
.principal(auth)
.param("param1", param1)
.param("param2", param2))
.andExpect(status().isForbidden())
.andReturn();
// Verify the error message is correct
final String msgKey = "error.code";
final String errorMsg = MessageFormat.format(resourceBundle.getString(msgKey), new Object[] {});
Assert.assertEquals("Failed to return proper error message.", errorMsg, result.getResponse().getContentAsString());
アサーションは、応答文字列と言って失敗しました空だった。応答をHttpServletResponseバッファに書き込む正しい方法は何ですか?
はい、私はあります。 「output.write(buffer、0、errorMsg.length())」の行を参照してください。 – Dave
いいえ、 'errorMsg.length()'バイトを 'buffer'から0に向かって出力して出力します。しかし、 'errorMsg'をコピーしたことがないので、' buffer'はすべて0です。 –