showBinaryContent
默認爲false,但二進制數據根據內容類型進行記錄。當前如果您的內容類型不是以下任何一種;二進制數據將被記錄。
static {
BINARY_CONTENT_MEDIA_TYPES = new ArrayList<String>();
BINARY_CONTENT_MEDIA_TYPES.add("application/octet-stream");
BINARY_CONTENT_MEDIA_TYPES.add("image/png");
BINARY_CONTENT_MEDIA_TYPES.add("image/jpeg");
BINARY_CONTENT_MEDIA_TYPES.add("image/gif");
}
說你的內容類型是application/zip
,你可以創建自定義攔截並覆蓋isBinaryContent如下圖所示
import org.apache.commons.lang3.StringUtils;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.interceptor.LoggingMessage;
public class KPLogOutInterceptor extends LoggingOutInterceptor {
@Override
public boolean isBinaryContent(String contentType) {
return contentType != null && (BINARY_CONTENT_MEDIA_TYPES.contains(contentType)|| "application/zip".equals(contentType);
}
}
的另一種方法,而無需使用內容類型如下圖所示。
import org.apache.commons.lang3.StringUtils;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.interceptor.LoggingMessage;
public class KPLogOutInterceptor extends LoggingOutInterceptor {
@Override
protected String formatLoggingMessage(LoggingMessage loggingMessage) {
return removePayload(loggingMessage.toString());
}
private String removePayload(String str){
StringBuilder builder = new StringBuilder(str);
if (str.indexOf("Payload:") + 9 > 8) {
builder.setLength(builder.indexOf("Payload:") + 8);
builder.append(" <content skipped>\n");
builder.append(StringUtils.repeat('-', 25));
}
return builder.toString();
}
}
非常感謝你,你的第二個建議非常有幫助! – MeanwhileInHell