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

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

Introduction

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

Prototype

List<MediaType> getSupportedMediaTypes();

Source Link

Document

Return the list of MediaType objects supported by this converter.

Usage

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

@SuppressWarnings("unchecked")
protected <T> void writeWithMessageConverters(T returnValue, HttpInputMessage inputMessage,
        HttpOutputMessage outputMessage) throws IOException, HttpMediaTypeNotAcceptableException {

    List<MediaType> acceptedMediaTypes = getAcceptedMediaTypes(inputMessage);

    List<MediaType> allSupportedMediaTypes = new ArrayList<MediaType>();
    if (this.messageConverters != null) {
        for (MediaType acceptedMediaType : acceptedMediaTypes) {
            for (HttpMessageConverter<?> messageConverter : this.messageConverters) {
                if (!messageConverter.canWrite(returnValue.getClass(), acceptedMediaType)) {
                    continue;
                }//from   w  ww  .ja  va 2  s  .c om
                ((HttpMessageConverter<T>) messageConverter).write(returnValue, acceptedMediaType,
                        outputMessage);
                if (logger.isDebugEnabled()) {
                    MediaType contentType = outputMessage.getHeaders().getContentType();
                    if (contentType == null) {
                        contentType = acceptedMediaType;
                    }
                    logger.debug("Written [" + returnValue + "] as \"" + contentType + "\" using ["
                            + messageConverter + "]");
                }
                return;
            }
        }
        for (HttpMessageConverter<?> messageConverter : messageConverters) {
            allSupportedMediaTypes.addAll(messageConverter.getSupportedMediaTypes());
        }
    }
    throw new HttpMediaTypeNotAcceptableException(allSupportedMediaTypes);
}