MIME types by Class for Encoder, Decoder, HttpMessageReader|Writer

Closes gh-26212
This commit is contained in:
Rossen Stoyanchev
2021-02-04 15:25:43 +00:00
parent 7cdaaa22bd
commit 0d16c9100a
14 changed files with 208 additions and 34 deletions

View File

@@ -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.
@@ -87,6 +87,10 @@ public class DecoderHttpMessageReader<T> implements HttpMessageReader<T> {
return this.mediaTypes;
}
@Override
public List<MediaType> getReadableMediaTypes(ResolvableType elementType) {
return MediaType.asMediaTypes(this.decoder.getDecodableMimeTypes(elementType));
}
@Override
public boolean canRead(ResolvableType elementType, @Nullable MediaType mediaType) {

View File

@@ -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.
@@ -105,6 +105,10 @@ public class EncoderHttpMessageWriter<T> implements HttpMessageWriter<T> {
return this.mediaTypes;
}
@Override
public List<MediaType> getWritableMediaTypes(ResolvableType elementType) {
return MediaType.asMediaTypes(getEncoder().getEncodableMimeTypes(elementType));
}
@Override
public boolean canWrite(ResolvableType elementType, @Nullable MediaType mediaType) {

View File

@@ -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.
@@ -16,6 +16,7 @@
package org.springframework.http.codec;
import java.util.Collections;
import java.util.List;
import java.util.Map;
@@ -43,10 +44,29 @@ import org.springframework.lang.Nullable;
public interface HttpMessageReader<T> {
/**
* Return the {@link MediaType}'s that this reader supports.
* Return the list of media types supported by this reader. The list may not
* apply to every possible target element type and calls to this method
* should typically be guarded via {@link #canRead(ResolvableType, MediaType)
* canWrite(elementType, null)}. The list may also exclude media types
* supported only for a specific element type. Alternatively, use
* {@link #getReadableMediaTypes(ResolvableType)} for a more precise list.
* @return the general list of supported media types
*/
List<MediaType> getReadableMediaTypes();
/**
* Return the list of media types supported by this Reader for the given type
* of element. This list may differ from {@link #getReadableMediaTypes()}
* if the Reader doesn't support the element type, or if it supports it
* only for a subset of media types.
* @param elementType the type of element to read
* @return the list of media types supported for the given class
* @since 5.3.4
*/
default List<MediaType> getReadableMediaTypes(ResolvableType elementType) {
return (canRead(elementType, null) ? getReadableMediaTypes() : Collections.emptyList());
}
/**
* Whether the given object type is supported by this reader.
* @param elementType the type of object to check
@@ -56,7 +76,7 @@ public interface HttpMessageReader<T> {
boolean canRead(ResolvableType elementType, @Nullable MediaType mediaType);
/**
* Read from the input message and encode to a stream of objects.
* Read from the input message and decode to a stream of objects.
* @param elementType the type of objects in the stream which must have been
* previously checked via {@link #canRead(ResolvableType, MediaType)}
* @param message the message to read from
@@ -66,7 +86,7 @@ public interface HttpMessageReader<T> {
Flux<T> read(ResolvableType elementType, ReactiveHttpInputMessage message, Map<String, Object> hints);
/**
* Read from the input message and encode to a single object.
* Read from the input message and decode to a single object.
* @param elementType the type of objects in the stream which must have been
* previously checked via {@link #canRead(ResolvableType, MediaType)}
* @param message the message to read from

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 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.
@@ -16,6 +16,7 @@
package org.springframework.http.codec;
import java.util.Collections;
import java.util.List;
import java.util.Map;
@@ -43,10 +44,29 @@ import org.springframework.lang.Nullable;
public interface HttpMessageWriter<T> {
/**
* Return the {@link MediaType}'s that this writer supports.
* Return the list of media types supported by this Writer. The list may not
* apply to every possible target element type and calls to this method should
* typically be guarded via {@link #canWrite(ResolvableType, MediaType)
* canWrite(elementType, null)}. The list may also exclude media types
* supported only for a specific element type. Alternatively, use
* {@link #getWritableMediaTypes(ResolvableType)} for a more precise list.
* @return the general list of supported media types
*/
List<MediaType> getWritableMediaTypes();
/**
* Return the list of media types supported by this Writer for the given type
* of element. This list may differ from {@link #getWritableMediaTypes()}
* if the Writer doesn't support the element type, or if it supports it
* only for a subset of media types.
* @param elementType the type of element to encode
* @return the list of media types supported for the given class
* @since 5.3.4
*/
default List<MediaType> getWritableMediaTypes(ResolvableType elementType) {
return (canWrite(elementType, null) ? getWritableMediaTypes() : Collections.emptyList());
}
/**
* Whether the given object type is supported by this writer.
* @param elementType the type of object to check

View File

@@ -259,6 +259,10 @@ public abstract class AbstractJackson2Decoder extends Jackson2CodecSupport imple
return getMimeTypes();
}
@Override
public List<MimeType> getDecodableMimeTypes(ResolvableType targetType) {
return getMimeTypes(targetType);
}
// Jackson2CodecSupport

View File

@@ -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.
@@ -357,6 +357,11 @@ public abstract class AbstractJackson2Encoder extends Jackson2CodecSupport imple
return getMimeTypes();
}
@Override
public List<MimeType> getEncodableMimeTypes(ResolvableType elementType) {
return getMimeTypes(elementType);
}
@Override
public List<MediaType> getStreamingMediaTypes() {
return Collections.unmodifiableList(this.streamingMediaTypes);

View File

@@ -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.
@@ -18,6 +18,7 @@ package org.springframework.http.codec.json;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
@@ -148,7 +149,7 @@ public abstract class Jackson2CodecSupport {
return Collections.emptyMap();
}
private Map<Class<?>, Map<MimeType, ObjectMapper>> getObjectMapperRegistrations() {
protected Map<Class<?>, Map<MimeType, ObjectMapper>> getObjectMapperRegistrations() {
return (this.objectMapperRegistrations != null ? this.objectMapperRegistrations : Collections.emptyMap());
}
@@ -159,6 +160,17 @@ public abstract class Jackson2CodecSupport {
return this.mimeTypes;
}
protected List<MimeType> getMimeTypes(ResolvableType elementType) {
Class<?> elementClass = elementType.toClass();
List<MimeType> result = null;
for (Map.Entry<Class<?>, Map<MimeType, ObjectMapper>> entry : getObjectMapperRegistrations().entrySet()) {
if (entry.getKey().isAssignableFrom(elementClass)) {
result = (result != null ? result : new ArrayList<>(entry.getValue().size()));
result.addAll(entry.getValue().keySet());
}
}
return (CollectionUtils.isEmpty(result) ? getMimeTypes() : result);
}
protected boolean supportsMimeType(@Nullable MimeType mimeType) {
if (mimeType == null) {