Example usage for org.springframework.web.util ContentCachingRequestWrapper getContentAsByteArray

List of usage examples for org.springframework.web.util ContentCachingRequestWrapper getContentAsByteArray

Introduction

In this page you can find the example usage for org.springframework.web.util ContentCachingRequestWrapper getContentAsByteArray.

Prototype

public byte[] getContentAsByteArray() 

Source Link

Document

Return the cached request content as a byte array.

Usage

From source file:org.springframework.cloud.function.web.flux.request.FluxHandlerMethodArgumentResolver.java

@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
        NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
    Object handler = webRequest.getAttribute(WebRequestConstants.HANDLER, NativeWebRequest.SCOPE_REQUEST);
    Class<?> type = inspector.getInputType(inspector.getName(handler));
    if (type == null) {
        type = Object.class;
    }/*from ww w.java 2 s .c  o  m*/
    List<Object> body;
    ContentCachingRequestWrapper nativeRequest = new ContentCachingRequestWrapper(
            webRequest.getNativeRequest(HttpServletRequest.class));
    if (logger.isDebugEnabled()) {
        logger.debug("Resolving request body into type: " + type);
    }
    if (isPlainText(webRequest) && CharSequence.class.isAssignableFrom(type)) {
        body = Arrays
                .asList(StreamUtils.copyToString(nativeRequest.getInputStream(), Charset.forName("UTF-8")));
    } else {
        try {
            body = mapper.readValue(nativeRequest.getInputStream(),
                    mapper.getTypeFactory().constructCollectionLikeType(ArrayList.class, type));
        } catch (JsonMappingException e) {
            nativeRequest.setAttribute(WebRequestConstants.INPUT_SINGLE, true);
            body = Arrays.asList(mapper.readValue(nativeRequest.getContentAsByteArray(), type));
        }
    }
    return new FluxRequest<Object>(body);
}