提交 54c2e866 编写于 作者: A Arjen Poutsma

Renamed getSupportedMimeTypes() in [En|De]coder

Renamed getSupportedMimeTypes() to getEncodableMimeTypes and
getDecodableMimeTypes. This will allow for both Encoder and Decoder to
be implemented in the same class.

This issue fixes #113.
上级 b5394a1f
......@@ -63,6 +63,6 @@ public interface Decoder<T> {
/**
* Return the list of MIME types this decoder supports.
*/
List<MimeType> getSupportedMimeTypes();
List<MimeType> getDecodableMimeTypes();
}
......@@ -66,6 +66,6 @@ public interface Encoder<T> {
/**
* Return the list of mime types this encoder supports.
*/
List<MimeType> getSupportedMimeTypes();
List<MimeType> getEncodableMimeTypes();
}
......@@ -26,19 +26,20 @@ import org.springframework.util.MimeType;
/**
* @author Sebastien Deleuze
* @author Arjen Poutsma
*/
public abstract class AbstractDecoder<T> implements Decoder<T> {
private List<MimeType> supportedMimeTypes = Collections.emptyList();
private List<MimeType> decodableMimeTypes = Collections.emptyList();
protected AbstractDecoder(MimeType... supportedMimeTypes) {
this.supportedMimeTypes = Arrays.asList(supportedMimeTypes);
this.decodableMimeTypes = Arrays.asList(supportedMimeTypes);
}
@Override
public List<MimeType> getSupportedMimeTypes() {
return this.supportedMimeTypes;
public List<MimeType> getDecodableMimeTypes() {
return this.decodableMimeTypes;
}
@Override
......@@ -46,12 +47,8 @@ public abstract class AbstractDecoder<T> implements Decoder<T> {
if (mimeType == null) {
return true;
}
for (MimeType supportedMimeType : this.supportedMimeTypes) {
if (supportedMimeType.isCompatibleWith(mimeType)) {
return true;
}
}
return false;
return this.decodableMimeTypes.stream().
anyMatch(mt -> mt.isCompatibleWith(mimeType));
}
}
......@@ -26,19 +26,20 @@ import org.springframework.util.MimeType;
/**
* @author Sebastien Deleuze
* @author Arjen Poutsma
*/
public abstract class AbstractEncoder<T> implements Encoder<T> {
private List<MimeType> supportedMimeTypes = Collections.emptyList();
private List<MimeType> encodableMimeTypes = Collections.emptyList();
protected AbstractEncoder(MimeType... supportedMimeTypes) {
this.supportedMimeTypes = Arrays.asList(supportedMimeTypes);
this.encodableMimeTypes = Arrays.asList(supportedMimeTypes);
}
@Override
public List<MimeType> getSupportedMimeTypes() {
return this.supportedMimeTypes;
public List<MimeType> getEncodableMimeTypes() {
return this.encodableMimeTypes;
}
@Override
......@@ -46,12 +47,8 @@ public abstract class AbstractEncoder<T> implements Encoder<T> {
if (mimeType == null) {
return true;
}
for (MimeType supportedMimeType : this.supportedMimeTypes) {
if (supportedMimeType.isCompatibleWith(mimeType)) {
return true;
}
}
return false;
return this.encodableMimeTypes.stream().
anyMatch(mt -> mt.isCompatibleWith(mimeType));
}
}
......@@ -18,7 +18,6 @@ package org.springframework.http.converter.reactive;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux;
......@@ -47,6 +46,10 @@ public class CodecHttpMessageConverter<T> implements HttpMessageConverter<T> {
private final Decoder<T> decoder;
private final List<MediaType> readableMediaTypes;
private final List<MediaType> writableMediaTypes;
/**
* Create a {@code CodecHttpMessageConverter} with the given {@link Encoder}. When
* using this constructor, all read-related methods will in {@code false} or an
......@@ -76,6 +79,13 @@ public class CodecHttpMessageConverter<T> implements HttpMessageConverter<T> {
public CodecHttpMessageConverter(Encoder<T> encoder, Decoder<T> decoder) {
this.encoder = encoder;
this.decoder = decoder;
this.readableMediaTypes = decoder != null ?
MediaTypeUtils.toMediaTypes(decoder.getDecodableMimeTypes()) :
Collections.emptyList();
this.writableMediaTypes = encoder != null ?
MediaTypeUtils.toMediaTypes(encoder.getEncodableMimeTypes()) :
Collections.emptyList();
}
@Override
......@@ -90,16 +100,12 @@ public class CodecHttpMessageConverter<T> implements HttpMessageConverter<T> {
@Override
public List<MediaType> getReadableMediaTypes() {
return this.decoder != null ? this.decoder.getSupportedMimeTypes().stream().
map(MediaTypeUtils::toMediaType).
collect(Collectors.toList()) : Collections.emptyList();
return this.readableMediaTypes;
}
@Override
public List<MediaType> getWritableMediaTypes() {
return this.encoder != null ? this.encoder.getSupportedMimeTypes().stream().
map(MediaTypeUtils::toMediaType).
collect(Collectors.toList()) : Collections.emptyList();
return this.writableMediaTypes;
}
@Override
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册