org.springframework.web.HttpMediaTypeException Java Examples

The following examples show how to use org.springframework.web.HttpMediaTypeException. 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: ProducesRequestCondition.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Checks if any of the contained media type expressions match the given
 * request 'Content-Type' header and returns an instance that is guaranteed
 * to contain matching expressions only. The match is performed via
 * {@link MediaType#isCompatibleWith(MediaType)}.
 * @param request the current request
 * @return the same instance if there are no expressions;
 * or a new condition with matching expressions;
 * or {@code null} if no expressions match.
 */
@Override
@Nullable
public ProducesRequestCondition getMatchingCondition(HttpServletRequest request) {
	if (CorsUtils.isPreFlightRequest(request)) {
		return EMPTY_CONDITION;
	}
	if (isEmpty()) {
		return this;
	}
	List<MediaType> acceptedMediaTypes;
	try {
		acceptedMediaTypes = getAcceptedMediaTypes(request);
	}
	catch (HttpMediaTypeException ex) {
		return null;
	}
	List<ProduceMediaTypeExpression> result = getMatchingExpressions(acceptedMediaTypes);
	if (!CollectionUtils.isEmpty(result)) {
		return new ProducesRequestCondition(result, this);
	}
	else if (MediaType.ALL.isPresentIn(acceptedMediaTypes)) {
		return EMPTY_CONDITION;
	}
	else {
		return null;
	}
}
 
Example #2
Source File: GlobalExceptionHandler.java    From spring-boot-plus with Apache License 2.0 5 votes vote down vote up
/**
 * HTTP
 *
 * @param exception
 * @return
 */
@ExceptionHandler(value = HttpMediaTypeException.class)
@ResponseStatus(HttpStatus.OK)
public ApiResult<Boolean> httpMediaTypeException(HttpMediaTypeException exception) {
    printRequestDetail();
    printApiCodeException(ApiCode.HTTP_MEDIA_TYPE_EXCEPTION, exception);
    return ApiResult.fail(ApiCode.HTTP_MEDIA_TYPE_EXCEPTION);
}
 
Example #3
Source File: ProducesRequestCondition.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Checks if any of the contained media type expressions match the given
 * request 'Content-Type' header and returns an instance that is guaranteed
 * to contain matching expressions only. The match is performed via
 * {@link MediaType#isCompatibleWith(MediaType)}.
 * @param request the current request
 * @return the same instance if there are no expressions;
 * or a new condition with matching expressions;
 * or {@code null} if no expressions match.
 */
@Override
@Nullable
public ProducesRequestCondition getMatchingCondition(HttpServletRequest request) {
	if (CorsUtils.isPreFlightRequest(request)) {
		return PRE_FLIGHT_MATCH;
	}
	if (isEmpty()) {
		return this;
	}

	List<MediaType> acceptedMediaTypes;
	try {
		acceptedMediaTypes = getAcceptedMediaTypes(request);
	}
	catch (HttpMediaTypeException ex) {
		return null;
	}

	Set<ProduceMediaTypeExpression> result = new LinkedHashSet<>(this.expressions);
	result.removeIf(expression -> !expression.match(acceptedMediaTypes));
	if (!result.isEmpty()) {
		return new ProducesRequestCondition(result, this.contentNegotiationManager);
	}
	else if (MediaType.ALL.isPresentIn(acceptedMediaTypes)) {
		return EMPTY_CONDITION;
	}
	else {
		return null;
	}
}
 
Example #4
Source File: ProducesRequestCondition.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Checks if any of the contained media type expressions match the given
 * request 'Content-Type' header and returns an instance that is guaranteed
 * to contain matching expressions only. The match is performed via
 * {@link MediaType#isCompatibleWith(MediaType)}.
 * @param request the current request
 * @return the same instance if there are no expressions;
 * or a new condition with matching expressions;
 * or {@code null} if no expressions match.
 */
@Override
public ProducesRequestCondition getMatchingCondition(HttpServletRequest request) {
	if (CorsUtils.isPreFlightRequest(request)) {
		return PRE_FLIGHT_MATCH;
	}
	if (isEmpty()) {
		return this;
	}
	List<MediaType> acceptedMediaTypes;
	try {
		acceptedMediaTypes = getAcceptedMediaTypes(request);
	}
	catch (HttpMediaTypeException ex) {
		return null;
	}
	Set<ProduceMediaTypeExpression> result = new LinkedHashSet<ProduceMediaTypeExpression>(expressions);
	for (Iterator<ProduceMediaTypeExpression> iterator = result.iterator(); iterator.hasNext();) {
		ProduceMediaTypeExpression expression = iterator.next();
		if (!expression.match(acceptedMediaTypes)) {
			iterator.remove();
		}
	}
	if (!result.isEmpty()) {
		return new ProducesRequestCondition(result, this.contentNegotiationManager);
	}
	else if (acceptedMediaTypes.contains(MediaType.ALL)) {
		return EMPTY_CONDITION;
	}
	else {
		return null;
	}
}
 
Example #5
Source File: RestErrorAdvice.java    From sample-boot-micro with MIT License 5 votes vote down vote up
/** メディアタイプのミスマッチ例外 */
@ExceptionHandler(HttpMediaTypeException.class)
public ResponseEntity<Map<String, String[]>> handleHttpMediaTypeException(
        HttpMediaTypeException e) {
    log.warn(e.getMessage());
    return new ErrorHolder(msg, locale(), "error.HttpMediaTypeNotAcceptable").result(HttpStatus.BAD_REQUEST);
}
 
Example #6
Source File: RestErrorAdvice.java    From sample-boot-hibernate with MIT License 5 votes vote down vote up
/** メディアタイプのミスマッチ例外 */
@ExceptionHandler(HttpMediaTypeException.class)
public ResponseEntity<Map<String, String[]>> handleHttpMediaTypeException(
        HttpMediaTypeException e) {
    log.warn(e.getMessage());
    return new ErrorHolder(msg, locale(), "error.HttpMediaTypeNotAcceptable").result(HttpStatus.BAD_REQUEST);
}
 
Example #7
Source File: GlobalDefaultExceptionHandler.java    From apollo with Apache License 2.0 4 votes vote down vote up
@ExceptionHandler({HttpRequestMethodNotSupportedException.class, HttpMediaTypeException.class})
public ResponseEntity<Map<String, Object>> badRequest(HttpServletRequest request,
                                                      ServletException ex) {
  return handleError(request, BAD_REQUEST, ex, WARN);
}
 
Example #8
Source File: ConsoleDefaultExceptionHandler.java    From x-pipe with Apache License 2.0 4 votes vote down vote up
@ExceptionHandler({HttpRequestMethodNotSupportedException.class, HttpMediaTypeException.class})
public ResponseEntity<Object> badRequest(HttpServletRequest request, ServletException ex) {
    return handleError(request, HttpStatus.BAD_REQUEST, ex);
  }
 
Example #9
Source File: KeeperExceptionHandler.java    From x-pipe with Apache License 2.0 4 votes vote down vote up
@ExceptionHandler({HttpRequestMethodNotSupportedException.class, HttpMediaTypeException.class})
public ResponseEntity<Object> badRequest(HttpServletRequest request,
                                         Throwable ex) {
    return handleError(request, BAD_REQUEST, ex);
}
 
Example #10
Source File: AbstractMediaTypeExpression.java    From spring4-understanding with Apache License 2.0 votes vote down vote up
protected abstract boolean matchMediaType(HttpServletRequest request) throws HttpMediaTypeException;