Example usage for org.springframework.http.converter HttpMessageConverter canRead

List of usage examples for org.springframework.http.converter HttpMessageConverter canRead

Introduction

In this page you can find the example usage for org.springframework.http.converter HttpMessageConverter canRead.

Prototype

boolean canRead(Class<?> clazz, @Nullable MediaType mediaType);

Source Link

Document

Indicates whether the given class can be read by this converter.

Usage

From source file:org.springframework.web.servlet.mvc.method.annotation.support.AbstractMessageConverterMethodProcessor.java

@SuppressWarnings("unchecked")
protected <T> Object readWithMessageConverters(NativeWebRequest webRequest, MethodParameter methodParam,
        Class<T> paramType) throws IOException, HttpMediaTypeNotSupportedException {

    HttpInputMessage inputMessage = createInputMessage(webRequest);

    MediaType contentType = inputMessage.getHeaders().getContentType();
    if (contentType == null) {
        StringBuilder builder = new StringBuilder(ClassUtils.getShortName(methodParam.getParameterType()));
        String paramName = methodParam.getParameterName();
        if (paramName != null) {
            builder.append(' ');
            builder.append(paramName);//from w w  w. j  ava  2 s.  c  om
        }
        throw new HttpMediaTypeNotSupportedException("Cannot read parameter (" + builder.toString()
                + ") using HttpMessageConverters: no Content-Type found in HTTP request");
    }

    List<MediaType> allSupportedMediaTypes = new ArrayList<MediaType>();
    if (this.messageConverters != null) {
        for (HttpMessageConverter<?> messageConverter : this.messageConverters) {
            allSupportedMediaTypes.addAll(messageConverter.getSupportedMediaTypes());
            if (messageConverter.canRead(paramType, contentType)) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Reading [" + paramType.getName() + "] as \"" + contentType + "\" using ["
                            + messageConverter + "]");
                }
                return ((HttpMessageConverter<T>) messageConverter).read(paramType, inputMessage);
            }
        }
    }

    throw new HttpMediaTypeNotSupportedException(contentType, allSupportedMediaTypes);
}