Example usage for org.springframework.web HttpMediaTypeNotSupportedException HttpMediaTypeNotSupportedException

List of usage examples for org.springframework.web HttpMediaTypeNotSupportedException HttpMediaTypeNotSupportedException

Introduction

In this page you can find the example usage for org.springframework.web HttpMediaTypeNotSupportedException HttpMediaTypeNotSupportedException.

Prototype

public HttpMediaTypeNotSupportedException(@Nullable MediaType contentType,
        List<MediaType> supportedMediaTypes) 

Source Link

Document

Create a new HttpMediaTypeNotSupportedException.

Usage

From source file:com.monarchapis.driver.spring.rest.ApiErrorResponseEntityExceptionHandlerTest.java

@Test
public void testHttpMediaTypeNotSupportedException() {
    List<MediaType> supportedMediaTypes = Lists.newArrayList(MediaType.APPLICATION_XML,
            MediaType.APPLICATION_JSON);
    ResponseEntity<Object> response = //
            performTest(new HttpMediaTypeNotSupportedException(MediaType.TEXT_PLAIN, supportedMediaTypes), //
                    400, "mediaTypeNotSupported");
    HttpHeaders headers = response.getHeaders();
    assertEquals(1, headers.size());//from   w w  w  .  j  av  a  2  s.c  o  m
    assertEquals(supportedMediaTypes, headers.getAccept());

    supportedMediaTypes = Collections.emptyList();
    response = //
            performTest(new HttpMediaTypeNotSupportedException(MediaType.TEXT_PLAIN, supportedMediaTypes), //
                    400, "mediaTypeNotSupported");
    headers = response.getHeaders();
    assertEquals(0, headers.size());
}

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

/**
 * Creates the method argument value of the expected parameter type by reading from the given HttpInputMessage. 
 * //from w ww .j a v  a  2 s .c  o m
 * @param <T> the expected type of the argument value to be created 
 * @param inputMessage the HTTP input message representing the current request
 * @param methodParam the method argument
 * @param paramType the type of the argument value to be created
 * @return the created method argument value
 * @throws IOException if the reading from the request fails
 * @throws HttpMediaTypeNotSupportedException if no suitable message converter is found
 */
@SuppressWarnings("unchecked")
protected <T> Object readWithMessageConverters(HttpInputMessage inputMessage, MethodParameter methodParam,
        Class<T> paramType) throws IOException, HttpMediaTypeNotSupportedException {

    MediaType contentType = inputMessage.getHeaders().getContentType();
    if (contentType == null) {
        contentType = MediaType.APPLICATION_OCTET_STREAM;
    }

    for (HttpMessageConverter<?> messageConverter : this.messageConverters) {
        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);
}