Java Code Examples for org.springframework.web.HttpMediaTypeNotSupportedException#getSupportedMediaTypes()

The following examples show how to use org.springframework.web.HttpMediaTypeNotSupportedException#getSupportedMediaTypes() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ResponseEntityExceptionHandler.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Customize the response for HttpMediaTypeNotSupportedException.
 * <p>This method sets the "Accept" header and delegates to
 * {@link #handleExceptionInternal}.
 * @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);
}
 
Example 2
Source File: ResponseEntityExceptionHandler.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Customize the response for HttpMediaTypeNotSupportedException.
 * <p>This method sets the "Accept" header and delegates to
 * {@link #handleExceptionInternal}.
 * @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);
}
 
Example 3
Source File: ResponseEntityExceptionHandler.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Customize the response for HttpMediaTypeNotSupportedException.
 * <p>This method sets the "Accept" header and delegates to
 * {@link #handleExceptionInternal}.
 * @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);
}
 
Example 4
Source File: ResponseEntityExceptionHandler.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Customize the response for HttpMediaTypeNotSupportedException.
 * <p>This method sets the "Accept" header and delegates to
 * {@link #handleExceptionInternal}.
 * @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);
}
 
Example 5
Source File: HttpMediaTypeNotSupportedExceptionHandler.java    From spring-rest-exception-handler with Apache License 2.0 5 votes vote down vote up
@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);
    }
    return headers;
}
 
Example 6
Source File: DefaultHandlerExceptionResolver.java    From spring-analysis-note with MIT License 3 votes vote down vote up
/**
 * 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
 * @param response current HTTP response
 * @param handler the executed handler
 * @return an empty ModelAndView indicating the exception was handled
 * @throws IOException potentially thrown from {@link HttpServletResponse#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();
}
 
Example 7
Source File: DefaultHandlerExceptionResolver.java    From java-technology-stack with MIT License 3 votes vote down vote up
/**
 * 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
 * @param response current HTTP response
 * @param handler the executed handler
 * @return an empty ModelAndView indicating the exception was handled
 * @throws IOException potentially thrown from {@link HttpServletResponse#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();
}
 
Example 8
Source File: DefaultHandlerExceptionResolver.java    From lams with GNU General Public License v2.0 3 votes vote down vote up
/**
 * 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
 * @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, 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();
}
 
Example 9
Source File: DefaultHandlerExceptionResolver.java    From spring4-understanding with Apache License 2.0 3 votes vote down vote up
/**
 * 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
 * @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, 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();
}