|
34 | 34 | import com.fasterxml.jackson.databind.ObjectWriter;
|
35 | 35 | import com.fasterxml.jackson.databind.SequenceWriter;
|
36 | 36 | import com.fasterxml.jackson.databind.exc.InvalidDefinitionException;
|
| 37 | +import com.fasterxml.jackson.databind.ser.FilterProvider; |
37 | 38 | import org.reactivestreams.Publisher;
|
38 | 39 | import reactor.core.publisher.Flux;
|
39 | 40 | import reactor.core.publisher.Mono;
|
|
48 | 49 | import org.springframework.core.log.LogFormatUtils;
|
49 | 50 | import org.springframework.http.MediaType;
|
50 | 51 | import org.springframework.http.codec.HttpMessageEncoder;
|
| 52 | +import org.springframework.http.converter.json.MappingJacksonValue; |
51 | 53 | import org.springframework.http.server.reactive.ServerHttpRequest;
|
52 | 54 | import org.springframework.http.server.reactive.ServerHttpResponse;
|
53 | 55 | import org.springframework.lang.Nullable;
|
@@ -148,7 +150,7 @@ public Flux<DataBuffer> encode(Publisher<?> inputStream, DataBufferFactory buffe
|
148 | 150 | byte[] separator = getStreamingMediaTypeSeparator(mimeType);
|
149 | 151 | if (separator != null) { // streaming
|
150 | 152 | try {
|
151 |
| - ObjectWriter writer = createObjectWriter(elementType, mimeType, hints); |
| 153 | + ObjectWriter writer = createObjectWriter(elementType, mimeType, null, hints); |
152 | 154 | ByteArrayBuilder byteBuilder = new ByteArrayBuilder(writer.getFactory()._getBufferRecycler());
|
153 | 155 | JsonEncoding encoding = getJsonEncoding(mimeType);
|
154 | 156 | JsonGenerator generator = getObjectMapper().getFactory().createGenerator(byteBuilder, encoding);
|
@@ -186,7 +188,18 @@ public Flux<DataBuffer> encode(Publisher<?> inputStream, DataBufferFactory buffe
|
186 | 188 | public DataBuffer encodeValue(Object value, DataBufferFactory bufferFactory,
|
187 | 189 | ResolvableType valueType, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {
|
188 | 190 |
|
189 |
| - ObjectWriter writer = createObjectWriter(valueType, mimeType, hints); |
| 191 | + Class<?> jsonView = null; |
| 192 | + FilterProvider filters = null; |
| 193 | + if (value instanceof MappingJacksonValue) { |
| 194 | + MappingJacksonValue container = (MappingJacksonValue) value; |
| 195 | + value = container.getValue(); |
| 196 | + jsonView = container.getSerializationView(); |
| 197 | + filters = container.getFilters(); |
| 198 | + } |
| 199 | + ObjectWriter writer = createObjectWriter(valueType, mimeType, jsonView, hints); |
| 200 | + if (filters != null) { |
| 201 | + writer = writer.with(filters); |
| 202 | + } |
190 | 203 | ByteArrayBuilder byteBuilder = new ByteArrayBuilder(writer.getFactory()._getBufferRecycler());
|
191 | 204 | try {
|
192 | 205 | JsonEncoding encoding = getJsonEncoding(mimeType);
|
@@ -268,10 +281,12 @@ private void logValue(@Nullable Map<String, Object> hints, Object value) {
|
268 | 281 | }
|
269 | 282 |
|
270 | 283 | private ObjectWriter createObjectWriter(ResolvableType valueType, @Nullable MimeType mimeType,
|
271 |
| - @Nullable Map<String, Object> hints) { |
| 284 | + @Nullable Class<?> jsonView, @Nullable Map<String, Object> hints) { |
272 | 285 |
|
273 | 286 | JavaType javaType = getJavaType(valueType.getType(), null);
|
274 |
| - Class<?> jsonView = (hints != null ? (Class<?>) hints.get(Jackson2CodecSupport.JSON_VIEW_HINT) : null); |
| 287 | + if (jsonView == null && hints != null) { |
| 288 | + jsonView = (Class<?>) hints.get(Jackson2CodecSupport.JSON_VIEW_HINT); |
| 289 | + } |
275 | 290 | ObjectWriter writer = (jsonView != null ?
|
276 | 291 | getObjectMapper().writerWithView(jsonView) : getObjectMapper().writer());
|
277 | 292 |
|
|
0 commit comments