本文整理汇总了Java中org.springframework.web.util.ContentCachingRequestWrapper.getContentAsByteArray方法的典型用法代码示例。如果您正苦于以下问题:Java ContentCachingRequestWrapper.getContentAsByteArray方法的具体用法?Java ContentCachingRequestWrapper.getContentAsByteArray怎么用?Java ContentCachingRequestWrapper.getContentAsByteArray使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.springframework.web.util.ContentCachingRequestWrapper
的用法示例。
在下文中一共展示了ContentCachingRequestWrapper.getContentAsByteArray方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: doFilterInternal
import org.springframework.web.util.ContentCachingRequestWrapper; //导入方法依赖的package包/类
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
ContentCachingRequestWrapper requestWrapper = new ContentCachingRequestWrapper(request);
ContentCachingResponseWrapper responseWrapper = new ContentCachingResponseWrapper(response);
try {
filterChain.doFilter(requestWrapper, responseWrapper);
} finally {
String requestBody = new String(requestWrapper.getContentAsByteArray());
log.info("Request body: {}", requestBody);
String responseBody = new String(responseWrapper.getContentAsByteArray());
log.info("Response body: {}", responseBody);
// Do not forget this line after reading response content or actual response will be empty!
responseWrapper.copyBodyToResponse();
}
}
示例2: extractRequestParameter
import org.springframework.web.util.ContentCachingRequestWrapper; //导入方法依赖的package包/类
/**
* 从HttpServletRequest中提取请求参数(包括请求体中的参数)
* @param request
* @param loggingContext
* @return
* @throws Exception
*/
protected HttpRequestParameter extractRequestParameter(HttpServletRequest request, LoggingContext loggingContext) throws Exception {
HttpRequestParameter parameter = new HttpRequestParameter();
Map<String,String[]> originalParamMap = request.getParameterMap();
Map<String,String> paramMap = new HashMap<String,String>();
if(originalParamMap != null && !originalParamMap.isEmpty()){
for(Map.Entry<String, String[]> entry : originalParamMap.entrySet()){
paramMap.put(entry.getKey(), getStringParameterValue(entry.getValue()));
}
}
parameter.setParameter(paramMap);
MediaType contentType = loggingContext.getHttpAccessLog().getRequestContentType();
if(contentType != null){
if(isContentCachingRequest(request)){
String charset = request.getCharacterEncoding();
if(StringUtils.isEmpty(charset)){
charset = StringUtils.defaultIfEmpty(contentType.getCharset().name(), GlobalConstants.SYSTEM_DEFAULT_CHARSET);
}
ContentCachingRequestWrapper requestToUse = (ContentCachingRequestWrapper) request;
byte[] bytes = requestToUse.getContentAsByteArray();
if(bytes != null){
String body = IOUtils.toString(bytes, charset);
Object bodyObj = body;
if(MediaType.APPLICATION_JSON.getType().equals(contentType.getType())){ //目前只处理JSON类型的数据
if(body.startsWith("[") && body.endsWith("]")){ //JSON Array String -> List<Map<String,Object>>
bodyObj = JsonUtils.json2Object(body, new TypeReference<List<Map<String,Object>>>(){});
}else if(body.startsWith("{") && body.endsWith("}")){ //JSON Object String -> Map<String,Object>
bodyObj = JsonUtils.json2Object(body, new TypeReference<Map<String,Object>>(){});
}
}
parameter.setBody(bodyObj);
}
}
}
return excludeRequestParameter(parameter, loggingContext);
}
示例3: doFilter
import org.springframework.web.util.ContentCachingRequestWrapper; //导入方法依赖的package包/类
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
ServletException {
if (!(request instanceof HttpServletRequest)) {
return;
}
ContentCachingRequestWrapper wrapperRequest = new ContentCachingRequestWrapper((HttpServletRequest) request);
ContentCachingResponseWrapper wrapperResponse = new ContentCachingResponseWrapper((HttpServletResponse)
response);
String ua = wrapperRequest.getHeader("User-Agent");
ua = null == ua ? "" : ua;
if (ua.equals("KeepAliveClient") || HttpMethod.HEAD.matches(wrapperRequest.getMethod())) {
chain.doFilter(wrapperRequest, wrapperResponse);
wrapperResponse.copyBodyToResponse();
} else {
ApplicationContext context = ACU.ctx();
logFilterInterceptorList.forEach(LogFilterInterceptor::prepare);
StringBuilder accessLogBuilder = new StringBuilder();
long startTime = Instant.now().toEpochMilli();
addLogKV(accessLogBuilder, "start", String.valueOf(startTime));
chain.doFilter(wrapperRequest, wrapperResponse);
addLogKV(accessLogBuilder, "status", String.valueOf(wrapperResponse.getStatusCode()));
RequestAttributes requestAttributes = (RequestAttributes) context.getBean("requestAttributes");
if (null != requestAttributes.getDeviceToken()) {
addLogKV(accessLogBuilder, "vd", (String) requestAttributes.getDeviceToken());
}
if (null != requestAttributes.getLoginUser()) {
addLogKV(accessLogBuilder, "user", String.valueOf(requestAttributes.getLoginUser().getId()));
addLogKV(accessLogBuilder, "token", requestAttributes.getLoginUser().getToken());
addLogKV(accessLogBuilder, "tokenExpireTime", requestAttributes.getLoginUser().getTokenExpireTime().toString
());
}
addLogKV(accessLogBuilder, "locale", RequestContextUtils.getLocale(wrapperRequest).toLanguageTag());
String remoteIp = requestAttributes.getRemoteIp();
addLogKV(accessLogBuilder, "prevIp", wrapperRequest.getRemoteAddr());
addLogKV(accessLogBuilder, "remoteIp", remoteIp);
addLogKV(accessLogBuilder, "request", wrapperRequest.getRequestURI());
addLogKV(accessLogBuilder, "method", wrapperRequest.getMethod());
addLogKV(accessLogBuilder, "params", parseParams(wrapperRequest.getParameterMap()));
addLogKV(accessLogBuilder, "ua", wrapperRequest.getHeader("User-Agent"));
if ("POST".equalsIgnoreCase(wrapperRequest.getMethod())) {
byte[] content = wrapperRequest.getContentAsByteArray();
if (content.length < 1) {
content = StreamUtils.copyToByteArray(wrapperRequest.getInputStream());
}
addLogKV(accessLogBuilder, "body", IOUtils.toString(content, "UTF-8").replaceAll("\\r\\n", "")
.replaceAll
("\\n", ""));
}
if (null != wrapperResponse.getContentType() && (wrapperResponse.getContentType().equals(MediaType
.APPLICATION_JSON_UTF8_VALUE) || wrapperResponse.getContentType().equals(MediaType
.APPLICATION_JSON_VALUE))) {
addLogKV(accessLogBuilder, "response", IOUtils.toString(wrapperResponse.getContentAsByteArray(),
"UTF-8"));
}
logFilterInterceptorList.forEach(logFilterInterceptor -> addLogKV(accessLogBuilder, logFilterInterceptor
.getKey(), logFilterInterceptor.getOutput()));
long endTime = Instant.now().toEpochMilli();
addLogKV(accessLogBuilder, "duration", String.valueOf(endTime - startTime) + "ms");
accessLogger.info(accessLogBuilder.toString());
wrapperResponse.copyBodyToResponse();
}
}