Media types by Class for HttpMessageConverter
See gh-26212
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2020 the original author or authors.
|
||||
* Copyright 2002-2021 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -339,7 +339,7 @@ final class DefaultEntityResponseBuilder<T> implements EntityResponse.Builder<T>
|
||||
|
||||
return messageConverters.stream()
|
||||
.filter(messageConverter -> messageConverter.canWrite(entityClass, null))
|
||||
.flatMap(messageConverter -> messageConverter.getSupportedMediaTypes().stream())
|
||||
.flatMap(messageConverter -> messageConverter.getSupportedMediaTypes(entityClass).stream())
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2020 the original author or authors.
|
||||
* Copyright 2002-2021 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -81,8 +81,6 @@ class DefaultServerRequest implements ServerRequest {
|
||||
|
||||
private final List<HttpMessageConverter<?>> messageConverters;
|
||||
|
||||
private final List<MediaType> allSupportedMediaTypes;
|
||||
|
||||
private final MultiValueMap<String, String> params;
|
||||
|
||||
private final Map<String, Object> attributes;
|
||||
@@ -94,7 +92,6 @@ class DefaultServerRequest implements ServerRequest {
|
||||
public DefaultServerRequest(HttpServletRequest servletRequest, List<HttpMessageConverter<?>> messageConverters) {
|
||||
this.serverHttpRequest = new ServletServerHttpRequest(servletRequest);
|
||||
this.messageConverters = Collections.unmodifiableList(new ArrayList<>(messageConverters));
|
||||
this.allSupportedMediaTypes = allSupportedMediaTypes(messageConverters);
|
||||
|
||||
this.headers = new DefaultRequestHeaders(this.serverHttpRequest.getHeaders());
|
||||
this.params = CollectionUtils.toMultiValueMap(new ServletParametersMap(servletRequest));
|
||||
@@ -107,13 +104,6 @@ class DefaultServerRequest implements ServerRequest {
|
||||
ServletRequestPathUtils.parseAndCache(servletRequest));
|
||||
}
|
||||
|
||||
private static List<MediaType> allSupportedMediaTypes(List<HttpMessageConverter<?>> messageConverters) {
|
||||
return messageConverters.stream()
|
||||
.flatMap(converter -> converter.getSupportedMediaTypes().stream())
|
||||
.sorted(MediaType.SPECIFICITY_COMPARATOR)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String methodName() {
|
||||
@@ -211,7 +201,14 @@ class DefaultServerRequest implements ServerRequest {
|
||||
return theConverter.read(clazz, this.serverHttpRequest);
|
||||
}
|
||||
}
|
||||
throw new HttpMediaTypeNotSupportedException(contentType, this.allSupportedMediaTypes);
|
||||
throw new HttpMediaTypeNotSupportedException(contentType, getSupportedMediaTypes(bodyClass));
|
||||
}
|
||||
|
||||
private List<MediaType> getSupportedMediaTypes(Class<?> bodyClass) {
|
||||
return this.messageConverters.stream()
|
||||
.flatMap(converter -> converter.getSupportedMediaTypes(bodyClass).stream())
|
||||
.sorted(MediaType.SPECIFICITY_COMPARATOR)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
* Copyright 2002-2021 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -23,7 +23,6 @@ import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.EnumSet;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
@@ -80,8 +79,6 @@ public abstract class AbstractMessageConverterMethodArgumentResolver implements
|
||||
|
||||
protected final List<HttpMessageConverter<?>> messageConverters;
|
||||
|
||||
protected final List<MediaType> allSupportedMediaTypes;
|
||||
|
||||
private final RequestResponseBodyAdviceChain advice;
|
||||
|
||||
|
||||
@@ -101,26 +98,10 @@ public abstract class AbstractMessageConverterMethodArgumentResolver implements
|
||||
|
||||
Assert.notEmpty(converters, "'messageConverters' must not be empty");
|
||||
this.messageConverters = converters;
|
||||
this.allSupportedMediaTypes = getAllSupportedMediaTypes(converters);
|
||||
this.advice = new RequestResponseBodyAdviceChain(requestResponseBodyAdvice);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return the media types supported by all provided message converters sorted
|
||||
* by specificity via {@link MediaType#sortBySpecificity(List)}.
|
||||
*/
|
||||
private static List<MediaType> getAllSupportedMediaTypes(List<HttpMessageConverter<?>> messageConverters) {
|
||||
Set<MediaType> allSupportedMediaTypes = new LinkedHashSet<>();
|
||||
for (HttpMessageConverter<?> messageConverter : messageConverters) {
|
||||
allSupportedMediaTypes.addAll(messageConverter.getSupportedMediaTypes());
|
||||
}
|
||||
List<MediaType> result = new ArrayList<>(allSupportedMediaTypes);
|
||||
MediaType.sortBySpecificity(result);
|
||||
return Collections.unmodifiableList(result);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return the configured {@link RequestBodyAdvice} and
|
||||
* {@link RequestBodyAdvice} where each instance may be wrapped as a
|
||||
@@ -222,7 +203,7 @@ public abstract class AbstractMessageConverterMethodArgumentResolver implements
|
||||
(noContentType && !message.hasBody())) {
|
||||
return null;
|
||||
}
|
||||
throw new HttpMediaTypeNotSupportedException(contentType, this.allSupportedMediaTypes);
|
||||
throw new HttpMediaTypeNotSupportedException(contentType, getSupportedMediaTypes(targetClass));
|
||||
}
|
||||
|
||||
MediaType selectedContentType = contentType;
|
||||
@@ -283,6 +264,21 @@ public abstract class AbstractMessageConverterMethodArgumentResolver implements
|
||||
return !hasBindingResult;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the media types supported by all provided message converters sorted
|
||||
* by specificity via {@link MediaType#sortBySpecificity(List)}.
|
||||
* @since 5.3.4
|
||||
*/
|
||||
protected List<MediaType> getSupportedMediaTypes(Class<?> clazz) {
|
||||
Set<MediaType> mediaTypeSet = new LinkedHashSet<>();
|
||||
for (HttpMessageConverter<?> converter : this.messageConverters) {
|
||||
mediaTypeSet.addAll(converter.getSupportedMediaTypes(clazz));
|
||||
}
|
||||
List<MediaType> result = new ArrayList<>(mediaTypeSet);
|
||||
MediaType.sortBySpecificity(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adapt the given argument against the method parameter, if necessary.
|
||||
* @param arg the resolved argument
|
||||
|
||||
@@ -299,7 +299,7 @@ public abstract class AbstractMessageConverterMethodProcessor extends AbstractMe
|
||||
throw new HttpMessageNotWritableException(
|
||||
"No converter for [" + valueType + "] with preset Content-Type '" + contentType + "'");
|
||||
}
|
||||
throw new HttpMediaTypeNotAcceptableException(this.allSupportedMediaTypes);
|
||||
throw new HttpMediaTypeNotAcceptableException(getSupportedMediaTypes(body.getClass()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -361,23 +361,18 @@ public abstract class AbstractMessageConverterMethodProcessor extends AbstractMe
|
||||
if (!CollectionUtils.isEmpty(mediaTypes)) {
|
||||
return new ArrayList<>(mediaTypes);
|
||||
}
|
||||
else if (!this.allSupportedMediaTypes.isEmpty()) {
|
||||
List<MediaType> result = new ArrayList<>();
|
||||
for (HttpMessageConverter<?> converter : this.messageConverters) {
|
||||
if (converter instanceof GenericHttpMessageConverter && targetType != null) {
|
||||
if (((GenericHttpMessageConverter<?>) converter).canWrite(targetType, valueClass, null)) {
|
||||
result.addAll(converter.getSupportedMediaTypes());
|
||||
}
|
||||
}
|
||||
else if (converter.canWrite(valueClass, null)) {
|
||||
result.addAll(converter.getSupportedMediaTypes());
|
||||
List<MediaType> result = new ArrayList<>();
|
||||
for (HttpMessageConverter<?> converter : this.messageConverters) {
|
||||
if (converter instanceof GenericHttpMessageConverter && targetType != null) {
|
||||
if (((GenericHttpMessageConverter<?>) converter).canWrite(targetType, valueClass, null)) {
|
||||
result.addAll(converter.getSupportedMediaTypes(valueClass));
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
else {
|
||||
return Collections.singletonList(MediaType.ALL);
|
||||
else if (converter.canWrite(valueClass, null)) {
|
||||
result.addAll(converter.getSupportedMediaTypes(valueClass));
|
||||
}
|
||||
}
|
||||
return (result.isEmpty() ? Collections.singletonList(MediaType.ALL) : result);
|
||||
}
|
||||
|
||||
private List<MediaType> getAcceptableMediaTypes(HttpServletRequest request)
|
||||
|
||||
Reference in New Issue
Block a user