Example usage for org.springframework.web HttpMediaTypeNotSupportedException getSupportedMediaTypes

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

Introduction

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

Prototype

public List<MediaType> getSupportedMediaTypes() 

Source Link

Document

Return the list of supported media types.

Usage

From source file:cz.jirutka.spring.exhandler.handlers.HttpMediaTypeNotSupportedExceptionHandler.java

@Override
protected HttpHeaders createHeaders(HttpMediaTypeNotSupportedException ex, HttpServletRequest req) {

    HttpHeaders headers = super.createHeaders(ex, req);
    List<MediaType> mediaTypes = ex.getSupportedMediaTypes();

    if (!isEmpty(mediaTypes)) {
        headers.setAccept(mediaTypes);//from   w  w w  . ja v  a  2  s  .c o  m
    }
    return headers;
}

From source file:business.CustomResponseEntityExceptionHandler.java

@Override
protected ResponseEntity<Object> handleHttpMediaTypeNotSupported(HttpMediaTypeNotSupportedException ex,
        HttpHeaders headers, HttpStatus status, WebRequest request) {
    String unsupported = "Unsupported content type: " + ex.getContentType();
    String supported = "Supported content types: " + MediaType.toString(ex.getSupportedMediaTypes());
    ErrorMessage errorMessage = new ErrorMessage(unsupported, supported);
    return new ResponseEntity<Object>(errorMessage, headers, status);
}

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

protected ResponseEntity<Object> handleHttpMediaTypeNotSupported(HttpMediaTypeNotSupportedException ex,
        HttpHeaders headers, HttpStatus status, WebRequest request) {
    List<MediaType> mediaTypes = ex.getSupportedMediaTypes();

    if (!CollectionUtils.isEmpty(mediaTypes)) {
        headers.setAccept(mediaTypes);//from w w w .  ja va 2 s.  c o m
    }

    return errorResponse("mediaTypeNotSupported", headers);
}

From source file:com.garyclayburg.persistence.repository.CustomResponseEntityExceptionHandler.java

@Override
protected ResponseEntity<Object> handleHttpMediaTypeNotSupported(HttpMediaTypeNotSupportedException ex,
        HttpHeaders headers, HttpStatus status, WebRequest request) {
    String unsupported = "Unsupported content type: " + ex.getContentType();
    String supported = "Supported content types: " + MediaType.toString(ex.getSupportedMediaTypes());
    ErrorMessage errorMessage = new ErrorMessage(unsupported, supported);
    return new ResponseEntity(errorMessage, headers, status);
}

From source file:com.ns.retailmgr.controller.exception.RestControllerExceptionHandler.java

@Override
protected ResponseEntity<Object> handleHttpMediaTypeNotSupported(HttpMediaTypeNotSupportedException ex,
        HttpHeaders headers, HttpStatus status, WebRequest request) {
    String unsupported = "Unsupported content type: " + ex.getContentType();
    String supported = "Supported content types: " + MediaType.toString(ex.getSupportedMediaTypes());
    RestErrorMessage RestErrorMessage = new RestErrorMessage(unsupported, supported);
    return new ResponseEntity(RestErrorMessage, headers, status);
}

From source file:be.bittich.quote.controller.impl.DefaultExceptionHandler.java

@RequestMapping(produces = APPLICATION_JSON)
@ExceptionHandler(HttpMediaTypeNotSupportedException.class)
@ResponseStatus(value = HttpStatus.UNSUPPORTED_MEDIA_TYPE)
public @ResponseBody Map<String, Object> handleUnsupportedMediaTypeException(
        HttpMediaTypeNotSupportedException ex) throws IOException {
    Map<String, Object> map = newHashMap();
    map.put("error", "Unsupported Media Type");
    map.put("cause", ex.getLocalizedMessage());
    map.put("supported", ex.getSupportedMediaTypes());
    return map;/*from   w ww.  ja v  a 2s  .com*/
}

From source file:net.jkratz.igdb.controller.advice.ErrorController.java

@RequestMapping(produces = "application/json")
@ExceptionHandler(HttpMediaTypeNotSupportedException.class)
@ResponseStatus(value = HttpStatus.UNSUPPORTED_MEDIA_TYPE)
public @ResponseBody Map<String, Object> handleUnsupportedMediaTypeException(
        HttpMediaTypeNotSupportedException ex) throws IOException {
    logger.warn(ex.getMessage());/*w  ww  . j  av a  2 s . c o  m*/
    Map<String, Object> map = Maps.newHashMap();
    map.put("error", "Unsupported Media Type");
    map.put("message", ex.getMessage());
    map.put("supported", ex.getSupportedMediaTypes());
    return map;
}

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

/**
 * Customize the response for HttpMediaTypeNotSupportedException.
 * This method sets the "Accept" header and delegates to
 * {@link #handleExceptionInternal(Exception, HttpHeaders, HttpStatus, WebRequest)}.
 * @param ex the exception/*from www .  java  2  s .c  o m*/
 * @param headers the headers to be written to the response
 * @param status the selected response status
 * @param request the current request
 * @return an Object or {@code null}
 */
protected Object handleHttpMediaTypeNotSupported(HttpMediaTypeNotSupportedException ex, HttpHeaders headers,
        HttpStatus status, WebRequest request) {

    List<MediaType> mediaTypes = ex.getSupportedMediaTypes();
    if (!CollectionUtils.isEmpty(mediaTypes)) {
        headers.setAccept(mediaTypes);
    }
    return handleExceptionInternal(ex, headers, status, request);
}

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

/**
 * Customize the response for HttpMediaTypeNotSupportedException.
 * <p>This method sets the "Accept" header and delegates to
 * {@link #handleExceptionInternal}.// w  ww .  ja v a 2 s.  co m
 * @param ex the exception
 * @param headers the headers to be written to the response
 * @param status the selected response status
 * @param request the current request
 * @return a {@code ResponseEntity} instance
 */
protected ResponseEntity<Object> handleHttpMediaTypeNotSupported(HttpMediaTypeNotSupportedException ex,
        HttpHeaders headers, HttpStatus status, WebRequest request) {

    List<MediaType> mediaTypes = ex.getSupportedMediaTypes();
    if (!CollectionUtils.isEmpty(mediaTypes)) {
        headers.setAccept(mediaTypes);
    }

    return handleExceptionInternal(ex, null, headers, status, request);
}

From source file:org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver.java

/**
 * Handle the case where no {@linkplain org.springframework.http.converter.HttpMessageConverter message converters}
 * were found for the PUT or POSTed content.
 * <p>The default implementation sends an HTTP 415 error, sets the "Accept" header,
 * and returns an empty {@code ModelAndView}. Alternatively, a fallback view could
 * be chosen, or the HttpMediaTypeNotSupportedException could be rethrown as-is.
 * @param ex the HttpMediaTypeNotSupportedException to be handled
 * @param request current HTTP request/* ww w  .jav a2s .c o m*/
 * @param response current HTTP response
 * @param handler the executed handler
 * @return an empty ModelAndView indicating the exception was handled
 * @throws IOException potentially thrown from response.sendError()
 */
protected ModelAndView handleHttpMediaTypeNotSupported(HttpMediaTypeNotSupportedException ex,
        HttpServletRequest request, HttpServletResponse response, @Nullable Object handler) throws IOException {

    response.sendError(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE);
    List<MediaType> mediaTypes = ex.getSupportedMediaTypes();
    if (!CollectionUtils.isEmpty(mediaTypes)) {
        response.setHeader("Accept", MediaType.toString(mediaTypes));
    }
    return new ModelAndView();
}