@ -21,6 +21,7 @@ import java.io.OutputStream;
import java.lang.annotation.Annotation ;
import java.lang.annotation.Annotation ;
import java.util.ArrayList ;
import java.util.ArrayList ;
import java.util.Collections ;
import java.util.Collections ;
import java.util.HashMap ;
import java.util.List ;
import java.util.List ;
import java.util.Map ;
import java.util.Map ;
@ -58,9 +59,18 @@ import org.springframework.util.MimeType;
* /
* /
public abstract class AbstractJackson2Encoder extends Jackson2CodecSupport implements HttpMessageEncoder < Object > {
public abstract class AbstractJackson2Encoder extends Jackson2CodecSupport implements HttpMessageEncoder < Object > {
protected final List < MediaType > streamingMediaTypes = new ArrayList < > ( 1 ) ;
private static final byte [ ] NEWLINE_SEPARATOR = { '\n' } ;
protected boolean streamingLineSeparator = true ;
private static final Map < MediaType , byte [ ] > STREAM_SEPARATORS ;
static {
STREAM_SEPARATORS = new HashMap < > ( ) ;
STREAM_SEPARATORS . put ( MediaType . APPLICATION_STREAM_JSON , NEWLINE_SEPARATOR ) ;
STREAM_SEPARATORS . put ( MediaType . parseMediaType ( "application/stream+x-jackson-smile" ) , new byte [ 0 ] ) ;
}
private final List < MediaType > streamingMediaTypes = new ArrayList < > ( 1 ) ;
/ * *
/ * *
@ -103,20 +113,23 @@ public abstract class AbstractJackson2Encoder extends Jackson2CodecSupport imple
return Flux . from ( inputStream ) . map ( value - >
return Flux . from ( inputStream ) . map ( value - >
encodeValue ( value , mimeType , bufferFactory , elementType , hints ) ) ;
encodeValue ( value , mimeType , bufferFactory , elementType , hints ) ) ;
}
}
else if ( this . streamingMediaTypes . stream ( ) . anyMatch ( mediaType - > mediaType . isCompatibleWith ( mimeType ) ) ) {
return Flux . from ( inputStream ) . map ( value - > {
for ( MediaType streamingMediaType : this . streamingMediaTypes ) {
DataBuffer buffer = encodeValue ( value , mimeType , bufferFactory , elementType , hints ) ;
if ( streamingMediaType . isCompatibleWith ( mimeType ) ) {
if ( streamingLineSeparator ) {
byte [ ] separator = STREAM_SEPARATORS . getOrDefault ( streamingMediaType , NEWLINE_SEPARATOR ) ;
buffer . write ( new byte [ ] { '\n' } ) ;
return Flux . from ( inputStream ) . map ( value - > {
}
DataBuffer buffer = encodeValue ( value , mimeType , bufferFactory , elementType , hints ) ;
return buffer ;
if ( separator ! = null ) {
} ) ;
buffer . write ( separator ) ;
}
}
else {
return buffer ;
ResolvableType listType = ResolvableType . forClassWithGenerics ( List . class , elementType ) ;
} ) ;
return Flux . from ( inputStream ) . collectList ( ) . map ( list - >
}
encodeValue ( list , mimeType , bufferFactory , listType , hints ) ) . flux ( ) ;
}
}
ResolvableType listType = ResolvableType . forClassWithGenerics ( List . class , elementType ) ;
return Flux . from ( inputStream ) . collectList ( ) . map ( list - >
encodeValue ( list , mimeType , bufferFactory , listType , hints ) ) . flux ( ) ;
}
}
private DataBuffer encodeValue ( Object value , @Nullable MimeType mimeType , DataBufferFactory bufferFactory ,
private DataBuffer encodeValue ( Object value , @Nullable MimeType mimeType , DataBufferFactory bufferFactory ,