|
21 | 21 | import java.util.Map;
|
22 | 22 |
|
23 | 23 | import com.fasterxml.jackson.annotation.JsonView;
|
| 24 | +import org.reactivestreams.Publisher; |
| 25 | +import static org.springframework.core.codec.AbstractEncoder.FLUSHING_STRATEGY_HINT; |
| 26 | +import static org.springframework.core.codec.AbstractEncoder.FlushingStrategy.AFTER_EACH_ELEMENT; |
| 27 | +import reactor.core.publisher.Mono; |
24 | 28 |
|
25 | 29 | import org.springframework.core.MethodParameter;
|
26 | 30 | import org.springframework.core.ResolvableType;
|
| 31 | +import org.springframework.core.codec.AbstractEncoder; |
27 | 32 | import org.springframework.core.codec.Encoder;
|
28 | 33 | import org.springframework.http.MediaType;
|
| 34 | +import org.springframework.http.ReactiveHttpOutputMessage; |
29 | 35 | import org.springframework.http.codec.json.AbstractJackson2Codec;
|
30 | 36 | import org.springframework.http.server.reactive.ServerHttpRequest;
|
| 37 | +import org.springframework.http.server.reactive.ServerHttpResponse; |
31 | 38 |
|
32 | 39 | /**
|
33 |
| - * {@link ServerHttpMessageWriter} that resolves those annotation or request based Jackson 2 hints: |
34 |
| - * <ul> |
35 |
| - * <li>{@code @JsonView} annotated handler method</li> |
36 |
| - * </ul> |
| 40 | + * Jackson {@link ServerHttpMessageWriter} that resolves {@code @JsonView} annotated handler |
| 41 | + * method and deals with {@link AbstractEncoder#FLUSHING_STRATEGY_HINT}. |
37 | 42 | *
|
38 | 43 | * @author Sebastien Deleuze
|
39 | 44 | * @since 5.0
|
@@ -72,4 +77,27 @@ protected Map<String, Object> resolveWriteHints(ResolvableType streamType,
|
72 | 77 | return hints;
|
73 | 78 | }
|
74 | 79 |
|
| 80 | + @Override |
| 81 | + public Mono<Void> write(Publisher<?> inputStream, ResolvableType elementType, MediaType mediaType, |
| 82 | + ReactiveHttpOutputMessage outputMessage, Map<String, Object> hints) { |
| 83 | + |
| 84 | + if ((mediaType != null) && mediaType.isCompatibleWith(MediaType.APPLICATION_STREAM_JSON)) { |
| 85 | + Map<String, Object> hintsWithFlush = new HashMap<>(hints); |
| 86 | + hintsWithFlush.put(FLUSHING_STRATEGY_HINT, AFTER_EACH_ELEMENT); |
| 87 | + return super.write(inputStream, elementType, mediaType, outputMessage, hintsWithFlush); |
| 88 | + } |
| 89 | + return super.write(inputStream, elementType, mediaType, outputMessage, hints); |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public Mono<Void> write(Publisher<?> inputStream, ResolvableType streamType, ResolvableType elementType, |
| 94 | + MediaType mediaType, ServerHttpRequest request, ServerHttpResponse response, Map<String, Object> hints) { |
| 95 | + |
| 96 | + if ((mediaType != null) && mediaType.isCompatibleWith(MediaType.APPLICATION_STREAM_JSON)) { |
| 97 | + Map<String, Object> hintsWithFlush = new HashMap<>(hints); |
| 98 | + hintsWithFlush.put(FLUSHING_STRATEGY_HINT, AFTER_EACH_ELEMENT); |
| 99 | + return super.write(inputStream, streamType, elementType, mediaType, request, response, hintsWithFlush); |
| 100 | + } |
| 101 | + return super.write(inputStream, streamType, elementType, mediaType, request, response, hints); |
| 102 | + } |
75 | 103 | }
|
0 commit comments