私はカスタムインターセプタにログインするためのHttpServletResponseの返送コンテンツを取得したいと思います。開発環境はSpringブート1.5.6 + Java 8 +埋め込みTomcat 8.0.35で、返すコンテンツはRESTfulインターフェイスjson stringです。これはhttpを取得するためのコードです応答内容:Springブートで完全なHttpServletResponse応答本文を取得するには?
/**
* get Response return json content
*
* @param response
* @return
* @throws IOException
* @throws NoSuchFieldException
* @throws IllegalAccessException
*/
public String getResponseContent(HttpServletResponse response) throws IOException, NoSuchFieldException, IllegalAccessException {
String responseContent = null;
CoyoteOutputStream outputStream = (CoyoteOutputStream) response.getOutputStream();
Class<CoyoteOutputStream> coyoteOutputStreamClass = CoyoteOutputStream.class;
Field obField = coyoteOutputStreamClass.getDeclaredField("ob");
if (obField.getType().toString().endsWith("OutputBuffer")) {
obField.setAccessible(true);
org.apache.catalina.connector.OutputBuffer outputBuffer = (org.apache.catalina.connector.OutputBuffer) obField.get(outputStream);
Class<org.apache.catalina.connector.OutputBuffer> opb = org.apache.catalina.connector.OutputBuffer.class;
Field outputChunkField = opb.getDeclaredField("outputChunk");
outputChunkField.setAccessible(true);
if (outputChunkField.getType().toString().endsWith("ByteChunk")) {
ByteChunk bc = (ByteChunk) outputChunkField.get(outputBuffer);
Integer length = bc.getLength();
if (length == 0) return null;
responseContent = new String(bc.getBytes(), "UTF-8");
Integer responseLength = StringUtils.isBlank(responseContent) ? 0 : responseContent.length();
if (responseLength < length) {
responseContent = responseContent.substring(0, responseLength);
} else {
responseContent = responseContent.substring(0, length);
}
}
}
return responseContent;
}
レスポンスJSONが短い場合、well.Butを実行しているコードリターンJSONが長すぎるとき、responseContentにのみコンテンツを解析し、応答内容の一部を持つが(構文解析する必要がログインする前に失敗しましたjsonといくつかの値をデータベースに書き込む)。
どのように応答を調整し、完全な応答コンテンツを取得するには?
レスポンスサイズを計算し、tomcatレスポンス制限で確認しました – CHiRAG
これを読んでください:https://stackoverflow.com/questions/33744875/spring-boot-how-to-log-all-requests-and-responses-with -exceptions-in-single-pl –