Update Codec API to use Map<String, Object> for hints

Issue: SPR-14557
This commit is contained in:
Sebastien Deleuze
2016-09-12 11:08:01 +02:00
parent b88ed85994
commit b91867cf45
32 changed files with 212 additions and 173 deletions

View File

@@ -18,6 +18,7 @@ package org.springframework.core.codec;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Map;
import org.reactivestreams.Publisher; import org.reactivestreams.Publisher;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
@@ -49,7 +50,7 @@ public abstract class AbstractDecoder<T> implements Decoder<T> {
} }
@Override @Override
public boolean canDecode(ResolvableType elementType, MimeType mimeType, Object... hints) { public boolean canDecode(ResolvableType elementType, MimeType mimeType, Map<String, Object> hints) {
if (mimeType == null) { if (mimeType == null) {
return true; return true;
} }
@@ -58,7 +59,7 @@ public abstract class AbstractDecoder<T> implements Decoder<T> {
@Override @Override
public Mono<T> decodeToMono(Publisher<DataBuffer> inputStream, ResolvableType elementType, public Mono<T> decodeToMono(Publisher<DataBuffer> inputStream, ResolvableType elementType,
MimeType mimeType, Object... hints) { MimeType mimeType, Map<String, Object> hints) {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }

View File

@@ -18,6 +18,7 @@ package org.springframework.core.codec;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Map;
import org.springframework.core.ResolvableType; import org.springframework.core.ResolvableType;
import org.springframework.util.MimeType; import org.springframework.util.MimeType;
@@ -45,7 +46,7 @@ public abstract class AbstractEncoder<T> implements Encoder<T> {
} }
@Override @Override
public boolean canEncode(ResolvableType elementType, MimeType mimeType, Object... hints) { public boolean canEncode(ResolvableType elementType, MimeType mimeType, Map<String, Object> hints) {
if (mimeType == null) { if (mimeType == null) {
return true; return true;
} }

View File

@@ -16,6 +16,8 @@
package org.springframework.core.codec; package org.springframework.core.codec;
import java.util.Map;
import org.reactivestreams.Publisher; import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux; import reactor.core.publisher.Flux;
@@ -41,13 +43,13 @@ public abstract class AbstractSingleValueEncoder<T> extends AbstractEncoder<T> {
@Override @Override
public final Flux<DataBuffer> encode(Publisher<? extends T> inputStream, DataBufferFactory bufferFactory, public final Flux<DataBuffer> encode(Publisher<? extends T> inputStream, DataBufferFactory bufferFactory,
ResolvableType elementType, MimeType mimeType, Object... hints) { ResolvableType elementType, MimeType mimeType, Map<String, Object> hints) {
return Flux.from(inputStream). return Flux.from(inputStream).
take(1). take(1).
concatMap(t -> { concatMap(t -> {
try { try {
return encode(t, bufferFactory, elementType, mimeType); return encode(t, bufferFactory, elementType, mimeType, hints);
} }
catch (Exception ex) { catch (Exception ex) {
return Flux.error(ex); return Flux.error(ex);
@@ -66,6 +68,6 @@ public abstract class AbstractSingleValueEncoder<T> extends AbstractEncoder<T> {
* @throws Exception in case of errors * @throws Exception in case of errors
*/ */
protected abstract Flux<DataBuffer> encode(T t, DataBufferFactory dataBufferFactory, protected abstract Flux<DataBuffer> encode(T t, DataBufferFactory dataBufferFactory,
ResolvableType type, MimeType mimeType, Object... hints) throws Exception; ResolvableType type, MimeType mimeType, Map<String, Object> hints) throws Exception;
} }

View File

@@ -17,6 +17,7 @@
package org.springframework.core.codec; package org.springframework.core.codec;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.Map;
import org.reactivestreams.Publisher; import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux; import reactor.core.publisher.Flux;
@@ -42,14 +43,14 @@ public class ByteBufferDecoder extends AbstractDecoder<ByteBuffer> {
@Override @Override
public boolean canDecode(ResolvableType elementType, MimeType mimeType, Object... hints) { public boolean canDecode(ResolvableType elementType, MimeType mimeType, Map<String, Object> hints) {
Class<?> clazz = elementType.getRawClass(); Class<?> clazz = elementType.getRawClass();
return (super.canDecode(elementType, mimeType, hints) && ByteBuffer.class.isAssignableFrom(clazz)); return (super.canDecode(elementType, mimeType, hints) && ByteBuffer.class.isAssignableFrom(clazz));
} }
@Override @Override
public Flux<ByteBuffer> decode(Publisher<DataBuffer> inputStream, ResolvableType elementType, public Flux<ByteBuffer> decode(Publisher<DataBuffer> inputStream, ResolvableType elementType,
MimeType mimeType, Object... hints) { MimeType mimeType, Map<String, Object> hints) {
return Flux.from(inputStream).map((dataBuffer) -> { return Flux.from(inputStream).map((dataBuffer) -> {
ByteBuffer copy = ByteBuffer.allocate(dataBuffer.readableByteCount()); ByteBuffer copy = ByteBuffer.allocate(dataBuffer.readableByteCount());

View File

@@ -17,6 +17,7 @@
package org.springframework.core.codec; package org.springframework.core.codec;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.Map;
import org.reactivestreams.Publisher; import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux; import reactor.core.publisher.Flux;
@@ -41,7 +42,7 @@ public class ByteBufferEncoder extends AbstractEncoder<ByteBuffer> {
@Override @Override
public boolean canEncode(ResolvableType elementType, MimeType mimeType, Object... hints) { public boolean canEncode(ResolvableType elementType, MimeType mimeType, Map<String, Object> hints) {
Class<?> clazz = elementType.getRawClass(); Class<?> clazz = elementType.getRawClass();
return (super.canEncode(elementType, mimeType, hints) && ByteBuffer.class.isAssignableFrom(clazz)); return (super.canEncode(elementType, mimeType, hints) && ByteBuffer.class.isAssignableFrom(clazz));
} }
@@ -49,7 +50,7 @@ public class ByteBufferEncoder extends AbstractEncoder<ByteBuffer> {
@Override @Override
public Flux<DataBuffer> encode(Publisher<? extends ByteBuffer> inputStream, public Flux<DataBuffer> encode(Publisher<? extends ByteBuffer> inputStream,
DataBufferFactory bufferFactory, ResolvableType elementType, MimeType mimeType, DataBufferFactory bufferFactory, ResolvableType elementType, MimeType mimeType,
Object... hints) { Map<String, Object> hints) {
return Flux.from(inputStream).map(bufferFactory::wrap); return Flux.from(inputStream).map(bufferFactory::wrap);
} }

View File

@@ -20,6 +20,7 @@ import java.nio.ByteBuffer;
import java.nio.CharBuffer; import java.nio.CharBuffer;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Map;
import org.reactivestreams.Publisher; import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux; import reactor.core.publisher.Flux;
@@ -48,7 +49,7 @@ public class CharSequenceEncoder extends AbstractEncoder<CharSequence> {
@Override @Override
public boolean canEncode(ResolvableType elementType, MimeType mimeType, Object... hints) { public boolean canEncode(ResolvableType elementType, MimeType mimeType, Map<String, Object> hints) {
Class<?> clazz = elementType.getRawClass(); Class<?> clazz = elementType.getRawClass();
return (super.canEncode(elementType, mimeType, hints) && CharSequence.class.isAssignableFrom(clazz)); return (super.canEncode(elementType, mimeType, hints) && CharSequence.class.isAssignableFrom(clazz));
} }
@@ -56,7 +57,7 @@ public class CharSequenceEncoder extends AbstractEncoder<CharSequence> {
@Override @Override
public Flux<DataBuffer> encode(Publisher<? extends CharSequence> inputStream, public Flux<DataBuffer> encode(Publisher<? extends CharSequence> inputStream,
DataBufferFactory bufferFactory, ResolvableType elementType, DataBufferFactory bufferFactory, ResolvableType elementType,
MimeType mimeType, Object... hints) { MimeType mimeType, Map<String, Object> hints) {
Charset charset; Charset charset;
if (mimeType != null && mimeType.getCharset() != null) { if (mimeType != null && mimeType.getCharset() != null) {

View File

@@ -16,7 +16,9 @@
package org.springframework.core.codec; package org.springframework.core.codec;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map;
import org.reactivestreams.Publisher; import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux; import reactor.core.publisher.Flux;
@@ -42,10 +44,10 @@ public interface Decoder<T> {
* type of the source stream. * type of the source stream.
* @param elementType the target element type for the output stream * @param elementType the target element type for the output stream
* @param mimeType the mime type associated with the stream to decode * @param mimeType the mime type associated with the stream to decode
* @param hints additional information about how to do decode, optional * @param hints additional information about how to do encode
* @return {@code true} if supported, {@code false} otherwise * @return {@code true} if supported, {@code false} otherwise
*/ */
boolean canDecode(ResolvableType elementType, MimeType mimeType, Object... hints); boolean canDecode(ResolvableType elementType, MimeType mimeType, Map<String, Object> hints);
/** /**
* Decode a {@link DataBuffer} input stream into a Flux of {@code T}. * Decode a {@link DataBuffer} input stream into a Flux of {@code T}.
@@ -54,11 +56,11 @@ public interface Decoder<T> {
* this type must have been previously passed to the {@link #canDecode} * this type must have been previously passed to the {@link #canDecode}
* method and it must have returned {@code true}. * method and it must have returned {@code true}.
* @param mimeType the MIME type associated with the input stream, optional * @param mimeType the MIME type associated with the input stream, optional
* @param hints additional information about how to do decode, optional * @param hints additional information about how to do encode
* @return the output stream with decoded elements * @return the output stream with decoded elements
*/ */
Flux<T> decode(Publisher<DataBuffer> inputStream, ResolvableType elementType, Flux<T> decode(Publisher<DataBuffer> inputStream, ResolvableType elementType,
MimeType mimeType, Object... hints); MimeType mimeType, Map<String, Object> hints);
/** /**
* Decode a {@link DataBuffer} input stream into a Mono of {@code T}. * Decode a {@link DataBuffer} input stream into a Mono of {@code T}.
@@ -67,11 +69,11 @@ public interface Decoder<T> {
* this type must have been previously passed to the {@link #canDecode} * this type must have been previously passed to the {@link #canDecode}
* method and it must have returned {@code true}. * method and it must have returned {@code true}.
* @param mimeType the MIME type associated with the input stream, optional * @param mimeType the MIME type associated with the input stream, optional
* @param hints additional information about how to do decode, optional * @param hints additional information about how to do encode
* @return the output stream with the decoded element * @return the output stream with the decoded element
*/ */
Mono<T> decodeToMono(Publisher<DataBuffer> inputStream, ResolvableType elementType, Mono<T> decodeToMono(Publisher<DataBuffer> inputStream, ResolvableType elementType,
MimeType mimeType, Object... hints); MimeType mimeType, Map<String, Object> hints);
/** /**
* Return the list of MIME types this decoder supports. * Return the list of MIME types this decoder supports.

View File

@@ -16,7 +16,9 @@
package org.springframework.core.codec; package org.springframework.core.codec;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map;
import org.reactivestreams.Publisher; import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux; import reactor.core.publisher.Flux;
@@ -43,10 +45,10 @@ public interface Encoder<T> {
* type for the output stream. * type for the output stream.
* @param elementType the type of elements in the source stream * @param elementType the type of elements in the source stream
* @param mimeType the MIME type for the output stream * @param mimeType the MIME type for the output stream
* @param hints additional information about how to do encode, optional * @param hints additional information about how to do encode
* @return {@code true} if supported, {@code false} otherwise * @return {@code true} if supported, {@code false} otherwise
*/ */
boolean canEncode(ResolvableType elementType, MimeType mimeType, Object... hints); boolean canEncode(ResolvableType elementType, MimeType mimeType, Map<String, Object> hints);
/** /**
* Encode a stream of Objects of type {@code T} into a {@link DataBuffer} * Encode a stream of Objects of type {@code T} into a {@link DataBuffer}
@@ -59,11 +61,11 @@ public interface Encoder<T> {
* this type must have been previously passed to the {@link #canEncode} * this type must have been previously passed to the {@link #canEncode}
* method and it must have returned {@code true}. * method and it must have returned {@code true}.
* @param mimeType the MIME type for the output stream * @param mimeType the MIME type for the output stream
* @param hints additional information about how to do encode, optional * @param hints additional information about how to do encode
* @return the output stream * @return the output stream
*/ */
Flux<DataBuffer> encode(Publisher<? extends T> inputStream, DataBufferFactory bufferFactory, Flux<DataBuffer> encode(Publisher<? extends T> inputStream, DataBufferFactory bufferFactory,
ResolvableType elementType, MimeType mimeType, Object... hints); ResolvableType elementType, MimeType mimeType, Map<String, Object> hints);
/** /**
* Return the list of mime types this encoder supports. * Return the list of mime types this encoder supports.

View File

@@ -17,6 +17,7 @@
package org.springframework.core.codec; package org.springframework.core.codec;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.util.Map;
import org.reactivestreams.Publisher; import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux; import reactor.core.publisher.Flux;
@@ -45,7 +46,7 @@ public class ResourceDecoder extends AbstractDecoder<Resource> {
@Override @Override
public boolean canDecode(ResolvableType elementType, MimeType mimeType, Object... hints) { public boolean canDecode(ResolvableType elementType, MimeType mimeType, Map<String, Object> hints) {
Class<?> clazz = elementType.getRawClass(); Class<?> clazz = elementType.getRawClass();
return (InputStreamResource.class.equals(clazz) || return (InputStreamResource.class.equals(clazz) ||
clazz.isAssignableFrom(ByteArrayResource.class)) && clazz.isAssignableFrom(ByteArrayResource.class)) &&
@@ -54,7 +55,7 @@ public class ResourceDecoder extends AbstractDecoder<Resource> {
@Override @Override
public Flux<Resource> decode(Publisher<DataBuffer> inputStream, ResolvableType elementType, public Flux<Resource> decode(Publisher<DataBuffer> inputStream, ResolvableType elementType,
MimeType mimeType, Object... hints) { MimeType mimeType, Map<String, Object> hints) {
Class<?> clazz = elementType.getRawClass(); Class<?> clazz = elementType.getRawClass();

View File

@@ -18,6 +18,7 @@ package org.springframework.core.codec;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.Map;
import reactor.core.publisher.Flux; import reactor.core.publisher.Flux;
@@ -56,14 +57,14 @@ public class ResourceEncoder extends AbstractSingleValueEncoder<Resource> {
@Override @Override
public boolean canEncode(ResolvableType elementType, MimeType mimeType, Object... hints) { public boolean canEncode(ResolvableType elementType, MimeType mimeType, Map<String, Object> hints) {
Class<?> clazz = elementType.getRawClass(); Class<?> clazz = elementType.getRawClass();
return (super.canEncode(elementType, mimeType, hints) && Resource.class.isAssignableFrom(clazz)); return (super.canEncode(elementType, mimeType, hints) && Resource.class.isAssignableFrom(clazz));
} }
@Override @Override
protected Flux<DataBuffer> encode(Resource resource, DataBufferFactory dataBufferFactory, protected Flux<DataBuffer> encode(Resource resource, DataBufferFactory dataBufferFactory,
ResolvableType type, MimeType mimeType, Object... hints) throws IOException { ResolvableType type, MimeType mimeType, Map<String, Object> hints) throws IOException {
InputStream is = resource.getInputStream(); InputStream is = resource.getInputStream();
return DataBufferUtils.read(is, dataBufferFactory, bufferSize); return DataBufferUtils.read(is, dataBufferFactory, bufferSize);

View File

@@ -21,6 +21,7 @@ import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.function.IntPredicate; import java.util.function.IntPredicate;
import org.reactivestreams.Publisher; import org.reactivestreams.Publisher;
@@ -77,14 +78,14 @@ public class StringDecoder extends AbstractDecoder<String> {
@Override @Override
public boolean canDecode(ResolvableType elementType, MimeType mimeType, Object... hints) { public boolean canDecode(ResolvableType elementType, MimeType mimeType, Map<String, Object> hints) {
return (super.canDecode(elementType, mimeType, hints) && return (super.canDecode(elementType, mimeType, hints) &&
String.class.equals(elementType.getRawClass())); String.class.equals(elementType.getRawClass()));
} }
@Override @Override
public Flux<String> decode(Publisher<DataBuffer> inputStream, ResolvableType elementType, public Flux<String> decode(Publisher<DataBuffer> inputStream, ResolvableType elementType,
MimeType mimeType, Object... hints) { MimeType mimeType, Map<String, Object> hints) {
Flux<DataBuffer> inputFlux = Flux.from(inputStream); Flux<DataBuffer> inputFlux = Flux.from(inputStream);
if (this.splitOnNewline) { if (this.splitOnNewline) {
@@ -95,7 +96,7 @@ public class StringDecoder extends AbstractDecoder<String> {
@Override @Override
public Mono<String> decodeToMono(Publisher<DataBuffer> inputStream, ResolvableType elementType, public Mono<String> decodeToMono(Publisher<DataBuffer> inputStream, ResolvableType elementType,
MimeType mimeType, Object... hints) { MimeType mimeType, Map<String, Object> hints) {
return Flux.from(inputStream) return Flux.from(inputStream)
.reduce(DataBuffer::write) .reduce(DataBuffer::write)

View File

@@ -17,6 +17,7 @@
package org.springframework.core.codec; package org.springframework.core.codec;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.Collections;
import org.junit.Test; import org.junit.Test;
import org.reactivestreams.Publisher; import org.reactivestreams.Publisher;
@@ -40,9 +41,12 @@ public class ByteBufferDecoderTests extends AbstractDataBufferAllocatingTestCase
@Test @Test
public void canDecode() { public void canDecode() {
assertTrue(this.decoder.canDecode(ResolvableType.forClass(ByteBuffer.class), MimeTypeUtils.TEXT_PLAIN)); assertTrue(this.decoder.canDecode(ResolvableType.forClass(ByteBuffer.class),
assertFalse(this.decoder.canDecode(ResolvableType.forClass(Integer.class), MimeTypeUtils.TEXT_PLAIN)); MimeTypeUtils.TEXT_PLAIN, Collections.emptyMap()));
assertTrue(this.decoder.canDecode(ResolvableType.forClass(ByteBuffer.class), MimeTypeUtils.APPLICATION_JSON)); assertFalse(this.decoder.canDecode(ResolvableType.forClass(Integer.class),
MimeTypeUtils.TEXT_PLAIN, Collections.emptyMap()));
assertTrue(this.decoder.canDecode(ResolvableType.forClass(ByteBuffer.class),
MimeTypeUtils.APPLICATION_JSON, Collections.emptyMap()));
} }
@Test @Test
@@ -52,7 +56,7 @@ public class ByteBufferDecoderTests extends AbstractDataBufferAllocatingTestCase
Flux<DataBuffer> source = Flux.just(fooBuffer, barBuffer); Flux<DataBuffer> source = Flux.just(fooBuffer, barBuffer);
Flux<ByteBuffer> output = this.decoder.decode(source, Flux<ByteBuffer> output = this.decoder.decode(source,
ResolvableType.forClassWithGenerics(Publisher.class, ByteBuffer.class), ResolvableType.forClassWithGenerics(Publisher.class, ByteBuffer.class),
null); null, Collections.emptyMap());
TestSubscriber TestSubscriber
.subscribe(output) .subscribe(output)
.assertNoError() .assertNoError()

View File

@@ -18,6 +18,7 @@ package org.springframework.core.codec;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Collections;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@@ -48,9 +49,12 @@ public class ByteBufferEncoderTests extends AbstractDataBufferAllocatingTestCase
@Test @Test
public void canEncode() { public void canEncode() {
assertTrue(this.encoder.canEncode(ResolvableType.forClass(ByteBuffer.class), MimeTypeUtils.TEXT_PLAIN)); assertTrue(this.encoder.canEncode(ResolvableType.forClass(ByteBuffer.class),
assertFalse(this.encoder.canEncode(ResolvableType.forClass(Integer.class), MimeTypeUtils.TEXT_PLAIN)); MimeTypeUtils.TEXT_PLAIN, Collections.emptyMap()));
assertTrue(this.encoder.canEncode(ResolvableType.forClass(ByteBuffer.class), MimeTypeUtils.APPLICATION_JSON)); assertFalse(this.encoder.canEncode(ResolvableType.forClass(Integer.class),
MimeTypeUtils.TEXT_PLAIN, Collections.emptyMap()));
assertTrue(this.encoder.canEncode(ResolvableType.forClass(ByteBuffer.class),
MimeTypeUtils.APPLICATION_JSON, Collections.emptyMap()));
} }
@Test @Test
@@ -62,7 +66,7 @@ public class ByteBufferEncoderTests extends AbstractDataBufferAllocatingTestCase
Flux<DataBuffer> output = this.encoder.encode(source, this.bufferFactory, Flux<DataBuffer> output = this.encoder.encode(source, this.bufferFactory,
ResolvableType.forClassWithGenerics(Publisher.class, ByteBuffer.class), ResolvableType.forClassWithGenerics(Publisher.class, ByteBuffer.class),
null); null, Collections.emptyMap());
TestSubscriber TestSubscriber
.subscribe(output) .subscribe(output)
.assertValuesWith(b -> { .assertValuesWith(b -> {

View File

@@ -16,6 +16,8 @@
package org.springframework.core.codec; package org.springframework.core.codec;
import java.util.Collections;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@@ -46,18 +48,23 @@ public class CharSequenceEncoderTests extends AbstractDataBufferAllocatingTestCa
@Test @Test
public void canWrite() { public void canWrite() {
assertTrue(this.encoder.canEncode(ResolvableType.forClass(String.class), MimeTypeUtils.TEXT_PLAIN)); assertTrue(this.encoder.canEncode(ResolvableType.forClass(String.class),
assertTrue(this.encoder.canEncode(ResolvableType.forClass(StringBuilder.class), MimeTypeUtils.TEXT_PLAIN)); MimeTypeUtils.TEXT_PLAIN, Collections.emptyMap()));
assertTrue(this.encoder.canEncode(ResolvableType.forClass(StringBuffer.class), MimeTypeUtils.TEXT_PLAIN)); assertTrue(this.encoder.canEncode(ResolvableType.forClass(StringBuilder.class),
assertFalse(this.encoder.canEncode(ResolvableType.forClass(Integer.class), MimeTypeUtils.TEXT_PLAIN)); MimeTypeUtils.TEXT_PLAIN, Collections.emptyMap()));
assertFalse(this.encoder.canEncode(ResolvableType.forClass(String.class), MimeTypeUtils.APPLICATION_JSON)); assertTrue(this.encoder.canEncode(ResolvableType.forClass(StringBuffer.class),
MimeTypeUtils.TEXT_PLAIN, Collections.emptyMap()));
assertFalse(this.encoder.canEncode(ResolvableType.forClass(Integer.class),
MimeTypeUtils.TEXT_PLAIN, Collections.emptyMap()));
assertFalse(this.encoder.canEncode(ResolvableType.forClass(String.class),
MimeTypeUtils.APPLICATION_JSON, Collections.emptyMap()));
} }
@Test @Test
public void writeString() throws InterruptedException { public void writeString() throws InterruptedException {
Flux<String> stringFlux = Flux.just("foo"); Flux<String> stringFlux = Flux.just("foo");
Flux<DataBuffer> output = Flux.from( Flux<DataBuffer> output = Flux.from(
this.encoder.encode(stringFlux, this.bufferFactory, null, null)); this.encoder.encode(stringFlux, this.bufferFactory, null, null,Collections.emptyMap()));
TestSubscriber TestSubscriber
.subscribe(output) .subscribe(output)
.assertNoError() .assertNoError()
@@ -69,7 +76,7 @@ public class CharSequenceEncoderTests extends AbstractDataBufferAllocatingTestCa
public void writeStringBuilder() throws InterruptedException { public void writeStringBuilder() throws InterruptedException {
Flux<StringBuilder> stringBuilderFlux = Flux.just(new StringBuilder("foo")); Flux<StringBuilder> stringBuilderFlux = Flux.just(new StringBuilder("foo"));
Flux<DataBuffer> output = Flux.from( Flux<DataBuffer> output = Flux.from(
this.encoder.encode(stringBuilderFlux, this.bufferFactory, null, null)); this.encoder.encode(stringBuilderFlux, this.bufferFactory, null, null, Collections.emptyMap()));
TestSubscriber TestSubscriber
.subscribe(output) .subscribe(output)
.assertNoError() .assertNoError()

View File

@@ -17,6 +17,7 @@
package org.springframework.core.codec; package org.springframework.core.codec;
import java.io.IOException; import java.io.IOException;
import java.util.Collections;
import org.junit.Test; import org.junit.Test;
import reactor.core.publisher.Flux; import reactor.core.publisher.Flux;
@@ -44,14 +45,14 @@ public class ResourceDecoderTests extends AbstractDataBufferAllocatingTestCase {
@Test @Test
public void canDecode() throws Exception { public void canDecode() throws Exception {
assertTrue(this.decoder.canDecode( assertTrue(this.decoder.canDecode(ResolvableType.forClass(InputStreamResource.class),
ResolvableType.forClass(InputStreamResource.class), MimeTypeUtils.TEXT_PLAIN)); MimeTypeUtils.TEXT_PLAIN, Collections.emptyMap()));
assertTrue(this.decoder.canDecode( assertTrue(this.decoder.canDecode(ResolvableType.forClass(ByteArrayResource.class),
ResolvableType.forClass(ByteArrayResource.class), MimeTypeUtils.TEXT_PLAIN)); MimeTypeUtils.TEXT_PLAIN, Collections.emptyMap()));
assertTrue(this.decoder.canDecode( assertTrue(this.decoder.canDecode(ResolvableType.forClass(Resource.class),
ResolvableType.forClass(Resource.class), MimeTypeUtils.TEXT_PLAIN)); MimeTypeUtils.TEXT_PLAIN, Collections.emptyMap()));
assertTrue(this.decoder.canDecode( assertTrue(this.decoder.canDecode(ResolvableType.forClass(InputStreamResource.class),
ResolvableType.forClass(InputStreamResource.class), MimeTypeUtils.APPLICATION_JSON)); MimeTypeUtils.APPLICATION_JSON, Collections.emptyMap()));
} }
@Test @Test
@@ -61,7 +62,7 @@ public class ResourceDecoderTests extends AbstractDataBufferAllocatingTestCase {
Flux<DataBuffer> source = Flux.just(fooBuffer, barBuffer); Flux<DataBuffer> source = Flux.just(fooBuffer, barBuffer);
Flux<Resource> result = this.decoder Flux<Resource> result = this.decoder
.decode(source, ResolvableType.forClass(Resource.class), null); .decode(source, ResolvableType.forClass(Resource.class), null, Collections.emptyMap());
TestSubscriber TestSubscriber
.subscribe(result) .subscribe(result)

View File

@@ -17,6 +17,7 @@
package org.springframework.core.codec; package org.springframework.core.codec;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Collections;
import org.junit.Test; import org.junit.Test;
import reactor.core.publisher.Flux; import reactor.core.publisher.Flux;
@@ -42,14 +43,14 @@ public class ResourceEncoderTests extends AbstractDataBufferAllocatingTestCase {
@Test @Test
public void canEncode() throws Exception { public void canEncode() throws Exception {
assertTrue(this.encoder.canEncode( assertTrue(this.encoder.canEncode(ResolvableType.forClass(InputStreamResource.class),
ResolvableType.forClass(InputStreamResource.class), MimeTypeUtils.TEXT_PLAIN)); MimeTypeUtils.TEXT_PLAIN, Collections.emptyMap()));
assertTrue(this.encoder.canEncode( assertTrue(this.encoder.canEncode(ResolvableType.forClass(ByteArrayResource.class),
ResolvableType.forClass(ByteArrayResource.class), MimeTypeUtils.TEXT_PLAIN)); MimeTypeUtils.TEXT_PLAIN, Collections.emptyMap()));
assertTrue(this.encoder.canEncode( assertTrue(this.encoder.canEncode(ResolvableType.forClass(Resource.class),
ResolvableType.forClass(Resource.class), MimeTypeUtils.TEXT_PLAIN)); MimeTypeUtils.TEXT_PLAIN, Collections.emptyMap()));
assertTrue(this.encoder.canEncode( assertTrue(this.encoder.canEncode(ResolvableType.forClass(InputStreamResource.class),
ResolvableType.forClass(InputStreamResource.class), MimeTypeUtils.APPLICATION_JSON)); MimeTypeUtils.APPLICATION_JSON, Collections.emptyMap()));
} }
@Test @Test
@@ -61,7 +62,7 @@ public class ResourceEncoderTests extends AbstractDataBufferAllocatingTestCase {
Flux<DataBuffer> output = this.encoder.encode(source, this.bufferFactory, Flux<DataBuffer> output = this.encoder.encode(source, this.bufferFactory,
ResolvableType.forClass(Resource.class), ResolvableType.forClass(Resource.class),
null); null, Collections.emptyMap());
TestSubscriber TestSubscriber
.subscribe(output) .subscribe(output)

View File

@@ -16,6 +16,8 @@
package org.springframework.core.codec; package org.springframework.core.codec;
import java.util.Collections;
import org.junit.Test; import org.junit.Test;
import reactor.core.publisher.Flux; import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
@@ -41,18 +43,24 @@ public class StringDecoderTests extends AbstractDataBufferAllocatingTestCase {
@Test @Test
public void canDecode() { public void canDecode() {
assertTrue(this.decoder.canDecode(ResolvableType.forClass(String.class), MimeTypeUtils.TEXT_PLAIN)); assertTrue(this.decoder.canDecode(ResolvableType.forClass(String.class),
assertTrue(this.decoder.canDecode(ResolvableType.forClass(String.class), MimeTypeUtils.TEXT_HTML)); MimeTypeUtils.TEXT_PLAIN, Collections.emptyMap()));
assertTrue(this.decoder.canDecode(ResolvableType.forClass(String.class), MimeTypeUtils.APPLICATION_JSON)); assertTrue(this.decoder.canDecode(ResolvableType.forClass(String.class),
assertFalse(this.decoder.canDecode(ResolvableType.forClass(Integer.class), MimeTypeUtils.TEXT_PLAIN)); MimeTypeUtils.TEXT_HTML, Collections.emptyMap()));
assertFalse(this.decoder.canDecode(ResolvableType.forClass(Object.class), MimeTypeUtils.APPLICATION_JSON)); assertTrue(this.decoder.canDecode(ResolvableType.forClass(String.class),
MimeTypeUtils.APPLICATION_JSON, Collections.emptyMap()));
assertFalse(this.decoder.canDecode(ResolvableType.forClass(Integer.class),
MimeTypeUtils.TEXT_PLAIN, Collections.emptyMap()));
assertFalse(this.decoder.canDecode(ResolvableType.forClass(Object.class),
MimeTypeUtils.APPLICATION_JSON, Collections.emptyMap()));
} }
@Test @Test
public void decode() throws InterruptedException { public void decode() throws InterruptedException {
this.decoder = new StringDecoder(false); this.decoder = new StringDecoder(false);
Flux<DataBuffer> source = Flux.just(stringBuffer("foo"), stringBuffer("bar"), stringBuffer("baz")); Flux<DataBuffer> source = Flux.just(stringBuffer("foo"), stringBuffer("bar"), stringBuffer("baz"));
Flux<String> output = this.decoder.decode(source, ResolvableType.forClass(String.class), null); Flux<String> output = this.decoder.decode(source, ResolvableType.forClass(String.class),
null, Collections.emptyMap());
TestSubscriber.subscribe(output) TestSubscriber.subscribe(output)
.assertNoError() .assertNoError()
@@ -65,7 +73,8 @@ public class StringDecoderTests extends AbstractDataBufferAllocatingTestCase {
DataBuffer fooBar = stringBuffer("\nfoo\r\nbar\r"); DataBuffer fooBar = stringBuffer("\nfoo\r\nbar\r");
DataBuffer baz = stringBuffer("\nbaz"); DataBuffer baz = stringBuffer("\nbaz");
Flux<DataBuffer> source = Flux.just(fooBar, baz); Flux<DataBuffer> source = Flux.just(fooBar, baz);
Flux<String> output = decoder.decode(source, ResolvableType.forClass(String.class), null); Flux<String> output = decoder.decode(source, ResolvableType.forClass(String.class),
null, Collections.emptyMap());
TestSubscriber.subscribe(output) TestSubscriber.subscribe(output)
.assertNoError() .assertNoError()
@@ -75,7 +84,8 @@ public class StringDecoderTests extends AbstractDataBufferAllocatingTestCase {
@Test @Test
public void decodeEmptyFlux() throws InterruptedException { public void decodeEmptyFlux() throws InterruptedException {
Flux<DataBuffer> source = Flux.empty(); Flux<DataBuffer> source = Flux.empty();
Flux<String> output = this.decoder.decode(source, ResolvableType.forClass(String.class), null); Flux<String> output = this.decoder.decode(source, ResolvableType.forClass(String.class),
null, Collections.emptyMap());
TestSubscriber.subscribe(output) TestSubscriber.subscribe(output)
.assertNoError() .assertNoError()
@@ -86,7 +96,8 @@ public class StringDecoderTests extends AbstractDataBufferAllocatingTestCase {
@Test @Test
public void decodeEmptyString() throws InterruptedException { public void decodeEmptyString() throws InterruptedException {
Flux<DataBuffer> source = Flux.just(stringBuffer("")); Flux<DataBuffer> source = Flux.just(stringBuffer(""));
Flux<String> output = this.decoder.decode(source, ResolvableType.forClass(String.class), null); Flux<String> output = this.decoder.decode(source,
ResolvableType.forClass(String.class), null, Collections.emptyMap());
TestSubscriber.subscribe(output).assertValues(""); TestSubscriber.subscribe(output).assertValues("");
} }
@@ -95,7 +106,8 @@ public class StringDecoderTests extends AbstractDataBufferAllocatingTestCase {
public void decodeToMono() throws InterruptedException { public void decodeToMono() throws InterruptedException {
this.decoder = new StringDecoder(false); this.decoder = new StringDecoder(false);
Flux<DataBuffer> source = Flux.just(stringBuffer("foo"), stringBuffer("bar"), stringBuffer("baz")); Flux<DataBuffer> source = Flux.just(stringBuffer("foo"), stringBuffer("bar"), stringBuffer("baz"));
Mono<String> output = this.decoder.decodeToMono(source, ResolvableType.forClass(String.class), null); Mono<String> output = this.decoder.decodeToMono(source,
ResolvableType.forClass(String.class), null, Collections.emptyMap());
TestSubscriber.subscribe(output) TestSubscriber.subscribe(output)
.assertNoError() .assertNoError()
@@ -106,7 +118,8 @@ public class StringDecoderTests extends AbstractDataBufferAllocatingTestCase {
@Test @Test
public void decodeToMonoWithEmptyFlux() throws InterruptedException { public void decodeToMonoWithEmptyFlux() throws InterruptedException {
Flux<DataBuffer> source = Flux.empty(); Flux<DataBuffer> source = Flux.empty();
Mono<String> output = this.decoder.decodeToMono(source, ResolvableType.forClass(String.class), null); Mono<String> output = this.decoder.decodeToMono(source,
ResolvableType.forClass(String.class), null, Collections.emptyMap());
TestSubscriber.subscribe(output) TestSubscriber.subscribe(output)
.assertNoError() .assertNoError()

View File

@@ -16,6 +16,7 @@
package org.springframework.web.reactive.result.view; package org.springframework.web.reactive.result.view;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
@@ -123,7 +124,7 @@ public class HttpMessageWriterView implements View {
else if (map.size() == 1) { else if (map.size() == 1) {
return map.values().iterator().next(); return map.values().iterator().next();
} }
else if (getMessageWriter().canWrite(ResolvableType.forClass(Map.class), null)) { else if (getMessageWriter().canWrite(ResolvableType.forClass(Map.class), null, Collections.emptyMap())) {
return map; return map;
} }
else { else {
@@ -142,10 +143,10 @@ public class HttpMessageWriterView implements View {
protected boolean isEligibleAttribute(String attributeName, Object attributeValue) { protected boolean isEligibleAttribute(String attributeName, Object attributeValue) {
ResolvableType type = ResolvableType.forClass(attributeValue.getClass()); ResolvableType type = ResolvableType.forClass(attributeValue.getClass());
if (getModelKeys().isEmpty()) { if (getModelKeys().isEmpty()) {
return getMessageWriter().canWrite(type, null); return getMessageWriter().canWrite(type, null, Collections.emptyMap());
} }
if (getModelKeys().contains(attributeName)) { if (getModelKeys().contains(attributeName)) {
if (getMessageWriter().canWrite(type, null)) { if (getMessageWriter().canWrite(type, null, Collections.emptyMap())) {
return true; return true;
} }
throw new IllegalStateException( throw new IllegalStateException(
@@ -163,7 +164,8 @@ public class HttpMessageWriterView implements View {
Publisher<? extends T> stream = Mono.just((T) value); Publisher<? extends T> stream = Mono.just((T) value);
ResolvableType type = ResolvableType.forClass(value.getClass()); ResolvableType type = ResolvableType.forClass(value.getClass());
ServerHttpResponse response = exchange.getResponse(); ServerHttpResponse response = exchange.getResponse();
return ((HttpMessageWriter<T>) getMessageWriter()).write(stream, type, contentType, response); return ((HttpMessageWriter<T>) getMessageWriter()).write(stream, type, contentType,
response, Collections.emptyMap());
} }
} }

View File

@@ -19,6 +19,7 @@ package org.springframework.web.reactive.result.method.annotation;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlElement;
@@ -386,7 +387,7 @@ public class RequestMappingMessageConversionIntegrationTests extends AbstractReq
DataBufferFactory dataBufferFactory = new DefaultDataBufferFactory(); DataBufferFactory dataBufferFactory = new DefaultDataBufferFactory();
Jackson2JsonEncoder encoder = new Jackson2JsonEncoder(); Jackson2JsonEncoder encoder = new Jackson2JsonEncoder();
return encoder.encode(Mono.just(new Person("Robert")), dataBufferFactory, return encoder.encode(Mono.just(new Person("Robert")), dataBufferFactory,
ResolvableType.forClass(Person.class), JSON).map(DataBuffer::asByteBuffer); ResolvableType.forClass(Person.class), JSON, Collections.emptyMap()).map(DataBuffer::asByteBuffer);
} }
@GetMapping("/flux") @GetMapping("/flux")

View File

@@ -38,6 +38,8 @@ import org.springframework.util.MimeType;
*/ */
public class AbstractJackson2Codec { public class AbstractJackson2Codec {
public static final String JSON_VIEW_HINT = AbstractJackson2Codec.class.getName() + ".jsonView";
protected static final List<MimeType> JSON_MIME_TYPES = Arrays.asList( protected static final List<MimeType> JSON_MIME_TYPES = Arrays.asList(
new MimeType("application", "json", StandardCharsets.UTF_8), new MimeType("application", "json", StandardCharsets.UTF_8),
new MimeType("application", "*+json", StandardCharsets.UTF_8)); new MimeType("application", "*+json", StandardCharsets.UTF_8));

View File

@@ -18,8 +18,8 @@ package org.springframework.http.codec.json;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonView;
import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader; import com.fasterxml.jackson.databind.ObjectReader;
@@ -62,7 +62,7 @@ public class Jackson2JsonDecoder extends AbstractJackson2Codec implements Decode
@Override @Override
public boolean canDecode(ResolvableType elementType, MimeType mimeType, Object... hints) { public boolean canDecode(ResolvableType elementType, MimeType mimeType, Map<String, Object> hints) {
if (mimeType == null) { if (mimeType == null) {
return true; return true;
} }
@@ -76,7 +76,7 @@ public class Jackson2JsonDecoder extends AbstractJackson2Codec implements Decode
@Override @Override
public Flux<Object> decode(Publisher<DataBuffer> inputStream, ResolvableType elementType, public Flux<Object> decode(Publisher<DataBuffer> inputStream, ResolvableType elementType,
MimeType mimeType, Object... hints) { MimeType mimeType, Map<String, Object> hints) {
JsonObjectDecoder objectDecoder = this.fluxObjectDecoder; JsonObjectDecoder objectDecoder = this.fluxObjectDecoder;
return decodeInternal(objectDecoder, inputStream, elementType, mimeType, hints); return decodeInternal(objectDecoder, inputStream, elementType, mimeType, hints);
@@ -84,14 +84,14 @@ public class Jackson2JsonDecoder extends AbstractJackson2Codec implements Decode
@Override @Override
public Mono<Object> decodeToMono(Publisher<DataBuffer> inputStream, ResolvableType elementType, public Mono<Object> decodeToMono(Publisher<DataBuffer> inputStream, ResolvableType elementType,
MimeType mimeType, Object... hints) { MimeType mimeType, Map<String, Object> hints) {
JsonObjectDecoder objectDecoder = this.monoObjectDecoder; JsonObjectDecoder objectDecoder = this.monoObjectDecoder;
return decodeInternal(objectDecoder, inputStream, elementType, mimeType, hints).singleOrEmpty(); return decodeInternal(objectDecoder, inputStream, elementType, mimeType, hints).singleOrEmpty();
} }
private Flux<Object> decodeInternal(JsonObjectDecoder objectDecoder, Publisher<DataBuffer> inputStream, private Flux<Object> decodeInternal(JsonObjectDecoder objectDecoder, Publisher<DataBuffer> inputStream,
ResolvableType elementType, MimeType mimeType, Object[] hints) { ResolvableType elementType, MimeType mimeType, Map<String, Object> hints) {
Assert.notNull(inputStream, "'inputStream' must not be null"); Assert.notNull(inputStream, "'inputStream' must not be null");
Assert.notNull(elementType, "'elementType' must not be null"); Assert.notNull(elementType, "'elementType' must not be null");
@@ -102,14 +102,10 @@ public class Jackson2JsonDecoder extends AbstractJackson2Codec implements Decode
JavaType javaType = getJavaType(elementType.getType(), contextClass); JavaType javaType = getJavaType(elementType.getType(), contextClass);
ObjectReader reader; ObjectReader reader;
JsonView jsonView = (methodParam != null ? methodParam.getParameterAnnotation(JsonView.class) : null); Class<?> jsonView = (Class<?>)hints.get(AbstractJackson2Codec.JSON_VIEW_HINT);
if (jsonView != null) { if (jsonView != null) {
Class<?>[] classes = jsonView.value(); reader = this.mapper.readerWithView(jsonView).forType(javaType);
if (classes.length != 1) {
throw new IllegalArgumentException("@JsonView only supported for response body advice " +
"with exactly 1 class argument: " + methodParam);
}
reader = this.mapper.readerWithView(classes[0]).forType(javaType);
} }
else { else {
reader = this.mapper.readerFor(javaType); reader = this.mapper.readerFor(javaType);

View File

@@ -20,8 +20,8 @@ import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.List; import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonView;
import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter; import com.fasterxml.jackson.databind.ObjectWriter;
@@ -30,7 +30,6 @@ import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux; import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import org.springframework.core.MethodParameter;
import org.springframework.core.ResolvableType; import org.springframework.core.ResolvableType;
import org.springframework.core.codec.CodecException; import org.springframework.core.codec.CodecException;
import org.springframework.core.codec.Encoder; import org.springframework.core.codec.Encoder;
@@ -68,7 +67,7 @@ public class Jackson2JsonEncoder extends AbstractJackson2Codec implements Encode
@Override @Override
public boolean canEncode(ResolvableType elementType, MimeType mimeType, Object... hints) { public boolean canEncode(ResolvableType elementType, MimeType mimeType, Map<String, Object> hints) {
if (mimeType == null) { if (mimeType == null) {
return true; return true;
} }
@@ -82,14 +81,14 @@ public class Jackson2JsonEncoder extends AbstractJackson2Codec implements Encode
@Override @Override
public Flux<DataBuffer> encode(Publisher<?> inputStream, DataBufferFactory bufferFactory, public Flux<DataBuffer> encode(Publisher<?> inputStream, DataBufferFactory bufferFactory,
ResolvableType elementType, MimeType mimeType, Object... hints) { ResolvableType elementType, MimeType mimeType, Map<String, Object> hints) {
Assert.notNull(inputStream, "'inputStream' must not be null"); Assert.notNull(inputStream, "'inputStream' must not be null");
Assert.notNull(bufferFactory, "'bufferFactory' must not be null"); Assert.notNull(bufferFactory, "'bufferFactory' must not be null");
Assert.notNull(elementType, "'elementType' must not be null"); Assert.notNull(elementType, "'elementType' must not be null");
if (inputStream instanceof Mono) { if (inputStream instanceof Mono) {
return Flux.from(inputStream).map(value -> encodeValue(value, bufferFactory, elementType)); return Flux.from(inputStream).map(value -> encodeValue(value, bufferFactory, elementType, hints));
} }
Mono<DataBuffer> startArray = Mono.just(bufferFactory.wrap(START_ARRAY_BUFFER)); Mono<DataBuffer> startArray = Mono.just(bufferFactory.wrap(START_ARRAY_BUFFER));
@@ -98,31 +97,25 @@ public class Jackson2JsonEncoder extends AbstractJackson2Codec implements Encode
Flux<DataBuffer> array = Flux.from(inputStream) Flux<DataBuffer> array = Flux.from(inputStream)
.concatMap(value -> { .concatMap(value -> {
DataBuffer arraySeparator = bufferFactory.wrap(SEPARATOR_BUFFER); DataBuffer arraySeparator = bufferFactory.wrap(SEPARATOR_BUFFER);
return Flux.just(encodeValue(value, bufferFactory, elementType), arraySeparator); return Flux.just(encodeValue(value, bufferFactory, elementType, hints), arraySeparator);
}); });
return Flux.concat(startArray, array.skipLast(1), endArray); return Flux.concat(startArray, array.skipLast(1), endArray);
} }
private DataBuffer encodeValue(Object value, DataBufferFactory bufferFactory, ResolvableType type) { private DataBuffer encodeValue(Object value, DataBufferFactory bufferFactory,
ResolvableType type, Map<String, Object> hints) {
TypeFactory typeFactory = this.mapper.getTypeFactory(); TypeFactory typeFactory = this.mapper.getTypeFactory();
JavaType javaType = typeFactory.constructType(type.getType()); JavaType javaType = typeFactory.constructType(type.getType());
MethodParameter returnType =
(type.getSource() instanceof MethodParameter ? (MethodParameter) type.getSource() : null);
if (type.isInstance(value)) { if (type.isInstance(value)) {
javaType = getJavaType(type.getType(), null); javaType = getJavaType(type.getType(), null);
} }
ObjectWriter writer; ObjectWriter writer;
JsonView jsonView = (returnType != null ? returnType.getMethodAnnotation(JsonView.class) : null); Class<?> jsonView = (Class<?>)hints.get(AbstractJackson2Codec.JSON_VIEW_HINT);
if (jsonView != null) { if (jsonView != null) {
Class<?>[] classes = jsonView.value(); writer = this.mapper.writerWithView(jsonView);
if (classes.length != 1) {
throw new IllegalArgumentException("@JsonView only supported for response body advice " +
"with exactly 1 class argument: " + returnType);
}
writer = this.mapper.writerWithView(classes[0]);
} }
else { else {
writer = this.mapper.writer(); writer = this.mapper.writer();

View File

@@ -19,6 +19,7 @@ package org.springframework.http.codec.json;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.function.Function; import java.util.function.Function;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
@@ -96,7 +97,7 @@ class JsonObjectDecoder extends AbstractDecoder<DataBuffer> {
@Override @Override
public Flux<DataBuffer> decode(Publisher<DataBuffer> inputStream, ResolvableType elementType, public Flux<DataBuffer> decode(Publisher<DataBuffer> inputStream, ResolvableType elementType,
MimeType mimeType, Object... hints) { MimeType mimeType, Map<String, Object> hints) {
return Flux.from(inputStream) return Flux.from(inputStream)
.flatMap(new Function<DataBuffer, Publisher<? extends DataBuffer>>() { .flatMap(new Function<DataBuffer, Publisher<? extends DataBuffer>>() {

View File

@@ -18,6 +18,7 @@ package org.springframework.http.codec.xml;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.function.Function; import java.util.function.Function;
import javax.xml.XMLConstants; import javax.xml.XMLConstants;
import javax.xml.bind.JAXBElement; import javax.xml.bind.JAXBElement;
@@ -75,7 +76,7 @@ public class Jaxb2XmlDecoder extends AbstractDecoder<Object> {
@Override @Override
public boolean canDecode(ResolvableType elementType, MimeType mimeType, Object... hints) { public boolean canDecode(ResolvableType elementType, MimeType mimeType, Map<String, Object> hints) {
if (super.canDecode(elementType, mimeType, hints)) { if (super.canDecode(elementType, mimeType, hints)) {
Class<?> outputClass = elementType.getRawClass(); Class<?> outputClass = elementType.getRawClass();
return outputClass.isAnnotationPresent(XmlRootElement.class) || return outputClass.isAnnotationPresent(XmlRootElement.class) ||
@@ -88,11 +89,11 @@ public class Jaxb2XmlDecoder extends AbstractDecoder<Object> {
@Override @Override
public Flux<Object> decode(Publisher<DataBuffer> inputStream, ResolvableType elementType, public Flux<Object> decode(Publisher<DataBuffer> inputStream, ResolvableType elementType,
MimeType mimeType, Object... hints) { MimeType mimeType, Map<String, Object> hints) {
Class<?> outputClass = elementType.getRawClass(); Class<?> outputClass = elementType.getRawClass();
Flux<XMLEvent> xmlEventFlux = Flux<XMLEvent> xmlEventFlux =
this.xmlEventDecoder.decode(inputStream, null, mimeType); this.xmlEventDecoder.decode(inputStream, null, mimeType, hints);
QName typeName = toQName(outputClass); QName typeName = toQName(outputClass);
Flux<List<XMLEvent>> splitEvents = split(xmlEventFlux, typeName); Flux<List<XMLEvent>> splitEvents = split(xmlEventFlux, typeName);

View File

@@ -18,6 +18,7 @@ package org.springframework.http.codec.xml;
import java.io.OutputStream; import java.io.OutputStream;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Map;
import javax.xml.bind.JAXBException; import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller; import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlRootElement;
@@ -52,7 +53,7 @@ public class Jaxb2XmlEncoder extends AbstractSingleValueEncoder<Object> {
@Override @Override
public boolean canEncode(ResolvableType elementType, MimeType mimeType, Object... hints) { public boolean canEncode(ResolvableType elementType, MimeType mimeType, Map<String, Object> hints) {
if (super.canEncode(elementType, mimeType, hints)) { if (super.canEncode(elementType, mimeType, hints)) {
Class<?> outputClass = elementType.getRawClass(); Class<?> outputClass = elementType.getRawClass();
return (outputClass.isAnnotationPresent(XmlRootElement.class) || return (outputClass.isAnnotationPresent(XmlRootElement.class) ||
@@ -66,7 +67,7 @@ public class Jaxb2XmlEncoder extends AbstractSingleValueEncoder<Object> {
@Override @Override
protected Flux<DataBuffer> encode(Object value, DataBufferFactory dataBufferFactory, protected Flux<DataBuffer> encode(Object value, DataBufferFactory dataBufferFactory,
ResolvableType type, MimeType mimeType, Object... hints) { ResolvableType type, MimeType mimeType, Map<String, Object> hints) {
try { try {
DataBuffer buffer = dataBufferFactory.allocateBuffer(1024); DataBuffer buffer = dataBufferFactory.allocateBuffer(1024);
OutputStream outputStream = buffer.asOutputStream(); OutputStream outputStream = buffer.asOutputStream();

View File

@@ -19,6 +19,7 @@ package org.springframework.http.codec.xml;
import java.io.InputStream; import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.function.Function; import java.util.function.Function;
import javax.xml.stream.XMLEventReader; import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLInputFactory;
@@ -89,7 +90,7 @@ public class XmlEventDecoder extends AbstractDecoder<XMLEvent> {
@Override @Override
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public Flux<XMLEvent> decode(Publisher<DataBuffer> inputStream, ResolvableType elementType, public Flux<XMLEvent> decode(Publisher<DataBuffer> inputStream, ResolvableType elementType,
MimeType mimeType, Object... hints) { MimeType mimeType, Map<String, Object> hints) {
Flux<DataBuffer> flux = Flux.from(inputStream); Flux<DataBuffer> flux = Flux.from(inputStream);
if (useAalto && aaltoPresent) { if (useAalto && aaltoPresent) {

View File

@@ -18,7 +18,9 @@ package org.springframework.http.codec.json;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonView; import com.fasterxml.jackson.annotation.JsonView;
import org.junit.Test; import org.junit.Test;
@@ -46,15 +48,16 @@ public class Jackson2JsonDecoderTests extends AbstractDataBufferAllocatingTestCa
public void canDecode() { public void canDecode() {
Jackson2JsonDecoder decoder = new Jackson2JsonDecoder(); Jackson2JsonDecoder decoder = new Jackson2JsonDecoder();
assertTrue(decoder.canDecode(null, MediaType.APPLICATION_JSON)); assertTrue(decoder.canDecode(null, MediaType.APPLICATION_JSON, Collections.emptyMap()));
assertFalse(decoder.canDecode(null, MediaType.APPLICATION_XML)); assertFalse(decoder.canDecode(null, MediaType.APPLICATION_XML, Collections.emptyMap()));
} }
@Test @Test
public void decodePojo() { public void decodePojo() {
Flux<DataBuffer> source = Flux.just(stringBuffer("{\"foo\": \"foofoo\", \"bar\": \"barbar\"}")); Flux<DataBuffer> source = Flux.just(stringBuffer("{\"foo\": \"foofoo\", \"bar\": \"barbar\"}"));
ResolvableType elementType = ResolvableType.forClass(Pojo.class); ResolvableType elementType = ResolvableType.forClass(Pojo.class);
Flux<Object> flux = new Jackson2JsonDecoder().decode(source, elementType, null); Flux<Object> flux = new Jackson2JsonDecoder().decode(source, elementType, null,
Collections.emptyMap());
TestSubscriber.subscribe(flux).assertNoError().assertComplete(). TestSubscriber.subscribe(flux).assertNoError().assertComplete().
assertValues(new Pojo("foofoo", "barbar")); assertValues(new Pojo("foofoo", "barbar"));
@@ -67,7 +70,8 @@ public class Jackson2JsonDecoderTests extends AbstractDataBufferAllocatingTestCa
Method method = getClass().getDeclaredMethod("handle", List.class); Method method = getClass().getDeclaredMethod("handle", List.class);
ResolvableType elementType = ResolvableType.forMethodParameter(method, 0); ResolvableType elementType = ResolvableType.forMethodParameter(method, 0);
Mono<Object> mono = new Jackson2JsonDecoder().decodeToMono(source, elementType, null); Mono<Object> mono = new Jackson2JsonDecoder().decodeToMono(source, elementType,
null, Collections.emptyMap());
TestSubscriber.subscribe(mono).assertNoError().assertComplete(). TestSubscriber.subscribe(mono).assertNoError().assertComplete().
assertValues(Arrays.asList(new Pojo("f1", "b1"), new Pojo("f2", "b2"))); assertValues(Arrays.asList(new Pojo("f1", "b1"), new Pojo("f2", "b2")));
@@ -79,7 +83,8 @@ public class Jackson2JsonDecoderTests extends AbstractDataBufferAllocatingTestCa
"[{\"bar\":\"b1\",\"foo\":\"f1\"},{\"bar\":\"b2\",\"foo\":\"f2\"}]")); "[{\"bar\":\"b1\",\"foo\":\"f1\"},{\"bar\":\"b2\",\"foo\":\"f2\"}]"));
ResolvableType elementType = ResolvableType.forClass(Pojo.class); ResolvableType elementType = ResolvableType.forClass(Pojo.class);
Flux<Object> flux = new Jackson2JsonDecoder().decode(source, elementType, null); Flux<Object> flux = new Jackson2JsonDecoder().decode(source, elementType, null,
Collections.emptyMap());
TestSubscriber.subscribe(flux).assertNoError().assertComplete(). TestSubscriber.subscribe(flux).assertNoError().assertComplete().
assertValues(new Pojo("f1", "b1"), new Pojo("f2", "b2")); assertValues(new Pojo("f1", "b1"), new Pojo("f2", "b2"));
@@ -89,10 +94,10 @@ public class Jackson2JsonDecoderTests extends AbstractDataBufferAllocatingTestCa
public void jsonView() throws Exception { public void jsonView() throws Exception {
Flux<DataBuffer> source = Flux.just( Flux<DataBuffer> source = Flux.just(
stringBuffer("{\"withView1\" : \"with\", \"withView2\" : \"with\", \"withoutView\" : \"without\"}")); stringBuffer("{\"withView1\" : \"with\", \"withView2\" : \"with\", \"withoutView\" : \"without\"}"));
ResolvableType elementType = ResolvableType ResolvableType elementType = ResolvableType.forClass(JacksonViewBean.class);
.forMethodParameter(JacksonController.class.getMethod("foo", JacksonViewBean.class), 0); Map<String, Object> hints = Collections.singletonMap(Jackson2JsonDecoder.JSON_VIEW_HINT, MyJacksonView1.class);
Flux<JacksonViewBean> flux = new Jackson2JsonDecoder() Flux<JacksonViewBean> flux = new Jackson2JsonDecoder()
.decode(source, elementType, null).cast(JacksonViewBean.class); .decode(source, elementType, null, hints).cast(JacksonViewBean.class);
TestSubscriber TestSubscriber
.subscribe(flux) .subscribe(flux)
@@ -109,7 +114,8 @@ public class Jackson2JsonDecoderTests extends AbstractDataBufferAllocatingTestCa
public void decodeEmptyBodyToMono() throws Exception { public void decodeEmptyBodyToMono() throws Exception {
Flux<DataBuffer> source = Flux.empty(); Flux<DataBuffer> source = Flux.empty();
ResolvableType elementType = ResolvableType.forClass(Pojo.class); ResolvableType elementType = ResolvableType.forClass(Pojo.class);
Mono<Object> flux = new Jackson2JsonDecoder().decodeToMono(source, elementType, null); Mono<Object> flux = new Jackson2JsonDecoder().decodeToMono(source, elementType,
null, Collections.emptyMap());
TestSubscriber.subscribe(flux) TestSubscriber.subscribe(flux)
.assertNoError() .assertNoError()
@@ -163,12 +169,4 @@ public class Jackson2JsonDecoderTests extends AbstractDataBufferAllocatingTestCa
} }
} }
private static class JacksonController {
public JacksonViewBean foo(@JsonView(MyJacksonView1.class) JacksonViewBean bean) {
return bean;
}
}
} }

View File

@@ -16,6 +16,9 @@
package org.springframework.http.codec.json; package org.springframework.http.codec.json;
import java.util.Collections;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonTypeInfo; import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonTypeName; import com.fasterxml.jackson.annotation.JsonTypeName;
import com.fasterxml.jackson.annotation.JsonView; import com.fasterxml.jackson.annotation.JsonView;
@@ -42,8 +45,8 @@ public class Jackson2JsonEncoderTests extends AbstractDataBufferAllocatingTestCa
@Test @Test
public void canEncode() { public void canEncode() {
assertTrue(this.encoder.canEncode(null, MediaType.APPLICATION_JSON)); assertTrue(this.encoder.canEncode(null, MediaType.APPLICATION_JSON, Collections.emptyMap()));
assertFalse(this.encoder.canEncode(null, MediaType.APPLICATION_XML)); assertFalse(this.encoder.canEncode(null, MediaType.APPLICATION_XML, Collections.emptyMap()));
} }
@Test @Test
@@ -54,7 +57,7 @@ public class Jackson2JsonEncoderTests extends AbstractDataBufferAllocatingTestCa
new Pojo("foofoofoo", "barbarbar") new Pojo("foofoofoo", "barbarbar")
); );
ResolvableType type = ResolvableType.forClass(Pojo.class); ResolvableType type = ResolvableType.forClass(Pojo.class);
Flux<DataBuffer> output = this.encoder.encode(source, this.bufferFactory, type, null); Flux<DataBuffer> output = this.encoder.encode(source, this.bufferFactory, type, null, Collections.emptyMap());
TestSubscriber.subscribe(output) TestSubscriber.subscribe(output)
.assertComplete() .assertComplete()
@@ -74,7 +77,7 @@ public class Jackson2JsonEncoderTests extends AbstractDataBufferAllocatingTestCa
public void encodeWithType() { public void encodeWithType() {
Flux<ParentClass> source = Flux.just(new Foo(), new Bar()); Flux<ParentClass> source = Flux.just(new Foo(), new Bar());
ResolvableType type = ResolvableType.forClass(ParentClass.class); ResolvableType type = ResolvableType.forClass(ParentClass.class);
Flux<DataBuffer> output = this.encoder.encode(source, this.bufferFactory, type, null); Flux<DataBuffer> output = this.encoder.encode(source, this.bufferFactory, type, null, Collections.emptyMap());
TestSubscriber.subscribe(output) TestSubscriber.subscribe(output)
.assertComplete() .assertComplete()
@@ -93,8 +96,9 @@ public class Jackson2JsonEncoderTests extends AbstractDataBufferAllocatingTestCa
bean.setWithView2("with"); bean.setWithView2("with");
bean.setWithoutView("without"); bean.setWithoutView("without");
ResolvableType type = ResolvableType.forMethodReturnType(JacksonController.class.getMethod("foo")); ResolvableType type = ResolvableType.forClass(JacksonViewBean.class);
Flux<DataBuffer> output = this.encoder.encode(Mono.just(bean), this.bufferFactory, type, null); Map<String, Object> hints = Collections.singletonMap(Jackson2JsonEncoder.JSON_VIEW_HINT, MyJacksonView1.class);
Flux<DataBuffer> output = this.encoder.encode(Mono.just(bean), this.bufferFactory, type, null, hints);
TestSubscriber.subscribe(output) TestSubscriber.subscribe(output)
.assertComplete() .assertComplete()
@@ -157,13 +161,4 @@ public class Jackson2JsonEncoderTests extends AbstractDataBufferAllocatingTestCa
} }
} }
private static class JacksonController {
@JsonView(MyJacksonView1.class)
public JacksonViewBean foo() {
return null;
}
}
} }

View File

@@ -17,6 +17,7 @@
package org.springframework.http.codec.json; package org.springframework.http.codec.json;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Collections;
import org.junit.Test; import org.junit.Test;
import reactor.core.publisher.Flux; import reactor.core.publisher.Flux;
@@ -36,7 +37,7 @@ public class JsonObjectDecoderTests extends AbstractDataBufferAllocatingTestCase
Flux<DataBuffer> source = Flux<DataBuffer> source =
Flux.just(stringBuffer("{\"foo\": \"foofoo\", \"bar\": \"barbar\"}")); Flux.just(stringBuffer("{\"foo\": \"foofoo\", \"bar\": \"barbar\"}"));
Flux<String> output = Flux<String> output =
decoder.decode(source, null, null).map(JsonObjectDecoderTests::toString); decoder.decode(source, null, null, Collections.emptyMap()).map(JsonObjectDecoderTests::toString);
TestSubscriber TestSubscriber
.subscribe(output) .subscribe(output)
.assertValues("{\"foo\": \"foofoo\", \"bar\": \"barbar\"}"); .assertValues("{\"foo\": \"foofoo\", \"bar\": \"barbar\"}");
@@ -48,7 +49,7 @@ public class JsonObjectDecoderTests extends AbstractDataBufferAllocatingTestCase
Flux<DataBuffer> source = Flux.just(stringBuffer("{\"foo\": \"foofoo\""), Flux<DataBuffer> source = Flux.just(stringBuffer("{\"foo\": \"foofoo\""),
stringBuffer(", \"bar\": \"barbar\"}")); stringBuffer(", \"bar\": \"barbar\"}"));
Flux<String> output = Flux<String> output =
decoder.decode(source, null, null).map(JsonObjectDecoderTests::toString); decoder.decode(source, null, null, Collections.emptyMap()).map(JsonObjectDecoderTests::toString);
TestSubscriber TestSubscriber
.subscribe(output) .subscribe(output)
.assertValues("{\"foo\": \"foofoo\", \"bar\": \"barbar\"}"); .assertValues("{\"foo\": \"foofoo\", \"bar\": \"barbar\"}");
@@ -60,7 +61,7 @@ public class JsonObjectDecoderTests extends AbstractDataBufferAllocatingTestCase
Flux<DataBuffer> source = Flux.just(stringBuffer( Flux<DataBuffer> source = Flux.just(stringBuffer(
"[{\"foo\": \"foofoo\", \"bar\": \"barbar\"},{\"foo\": \"foofoofoo\", \"bar\": \"barbarbar\"}]")); "[{\"foo\": \"foofoo\", \"bar\": \"barbar\"},{\"foo\": \"foofoofoo\", \"bar\": \"barbarbar\"}]"));
Flux<String> output = Flux<String> output =
decoder.decode(source, null, null).map(JsonObjectDecoderTests::toString); decoder.decode(source, null, null, Collections.emptyMap()).map(JsonObjectDecoderTests::toString);
TestSubscriber TestSubscriber
.subscribe(output) .subscribe(output)
.assertValues("{\"foo\": \"foofoo\", \"bar\": \"barbar\"}", .assertValues("{\"foo\": \"foofoo\", \"bar\": \"barbar\"}",
@@ -74,7 +75,7 @@ public class JsonObjectDecoderTests extends AbstractDataBufferAllocatingTestCase
Flux.just(stringBuffer("[{\"foo\": \"foofoo\", \"bar\""), stringBuffer( Flux.just(stringBuffer("[{\"foo\": \"foofoo\", \"bar\""), stringBuffer(
": \"barbar\"},{\"foo\": \"foofoofoo\", \"bar\": \"barbarbar\"}]")); ": \"barbar\"},{\"foo\": \"foofoofoo\", \"bar\": \"barbarbar\"}]"));
Flux<String> output = Flux<String> output =
decoder.decode(source, null, null).map(JsonObjectDecoderTests::toString); decoder.decode(source, null, null, Collections.emptyMap()).map(JsonObjectDecoderTests::toString);
TestSubscriber TestSubscriber
.subscribe(output) .subscribe(output)
.assertValues("{\"foo\": \"foofoo\", \"bar\": \"barbar\"}", .assertValues("{\"foo\": \"foofoo\", \"bar\": \"barbar\"}",

View File

@@ -16,6 +16,7 @@
package org.springframework.http.codec.xml; package org.springframework.http.codec.xml;
import java.util.Collections;
import java.util.List; import java.util.List;
import javax.xml.namespace.QName; import javax.xml.namespace.QName;
import javax.xml.stream.events.XMLEvent; import javax.xml.stream.events.XMLEvent;
@@ -71,23 +72,23 @@ public class Jaxb2XmlDecoderTests extends AbstractDataBufferAllocatingTestCase {
@Test @Test
public void canDecode() { public void canDecode() {
assertTrue(this.decoder.canDecode(ResolvableType.forClass(Pojo.class), assertTrue(this.decoder.canDecode(ResolvableType.forClass(Pojo.class),
MediaType.APPLICATION_XML)); MediaType.APPLICATION_XML, Collections.emptyMap()));
assertTrue(this.decoder.canDecode(ResolvableType.forClass(Pojo.class), assertTrue(this.decoder.canDecode(ResolvableType.forClass(Pojo.class),
MediaType.TEXT_XML)); MediaType.TEXT_XML, Collections.emptyMap()));
assertFalse(this.decoder.canDecode(ResolvableType.forClass(Pojo.class), assertFalse(this.decoder.canDecode(ResolvableType.forClass(Pojo.class),
MediaType.APPLICATION_JSON)); MediaType.APPLICATION_JSON, Collections.emptyMap()));
assertTrue(this.decoder.canDecode(ResolvableType.forClass(TypePojo.class), assertTrue(this.decoder.canDecode(ResolvableType.forClass(TypePojo.class),
MediaType.APPLICATION_XML)); MediaType.APPLICATION_XML, Collections.emptyMap()));
assertFalse(this.decoder.canDecode(ResolvableType.forClass(getClass()), assertFalse(this.decoder.canDecode(ResolvableType.forClass(getClass()),
MediaType.APPLICATION_XML)); MediaType.APPLICATION_XML, Collections.emptyMap()));
} }
@Test @Test
public void splitOneBranches() { public void splitOneBranches() {
Flux<XMLEvent> xmlEvents = this.xmlEventDecoder Flux<XMLEvent> xmlEvents = this.xmlEventDecoder
.decode(Flux.just(stringBuffer(POJO_ROOT)), null, null); .decode(Flux.just(stringBuffer(POJO_ROOT)), null, null, Collections.emptyMap());
Flux<List<XMLEvent>> result = this.decoder.split(xmlEvents, new QName("pojo")); Flux<List<XMLEvent>> result = this.decoder.split(xmlEvents, new QName("pojo"));
TestSubscriber TestSubscriber
@@ -112,7 +113,7 @@ public class Jaxb2XmlDecoderTests extends AbstractDataBufferAllocatingTestCase {
@Test @Test
public void splitMultipleBranches() { public void splitMultipleBranches() {
Flux<XMLEvent> xmlEvents = this.xmlEventDecoder Flux<XMLEvent> xmlEvents = this.xmlEventDecoder
.decode(Flux.just(stringBuffer(POJO_CHILD)), null, null); .decode(Flux.just(stringBuffer(POJO_CHILD)), null, null, Collections.emptyMap());
Flux<List<XMLEvent>> result = this.decoder.split(xmlEvents, new QName("pojo")); Flux<List<XMLEvent>> result = this.decoder.split(xmlEvents, new QName("pojo"));
TestSubscriber TestSubscriber
@@ -160,8 +161,8 @@ public class Jaxb2XmlDecoderTests extends AbstractDataBufferAllocatingTestCase {
@Test @Test
public void decodeSingleXmlRootElement() throws Exception { public void decodeSingleXmlRootElement() throws Exception {
Flux<DataBuffer> source = Flux.just(stringBuffer(POJO_ROOT)); Flux<DataBuffer> source = Flux.just(stringBuffer(POJO_ROOT));
Flux<Object> output = Flux<Object> output = this.decoder.decode(source, ResolvableType.forClass(Pojo.class),
this.decoder.decode(source, ResolvableType.forClass(Pojo.class), null); null, Collections.emptyMap());
TestSubscriber TestSubscriber
.subscribe(output) .subscribe(output)
@@ -173,8 +174,8 @@ public class Jaxb2XmlDecoderTests extends AbstractDataBufferAllocatingTestCase {
@Test @Test
public void decodeSingleXmlTypeElement() throws Exception { public void decodeSingleXmlTypeElement() throws Exception {
Flux<DataBuffer> source = Flux.just(stringBuffer(POJO_ROOT)); Flux<DataBuffer> source = Flux.just(stringBuffer(POJO_ROOT));
Flux<Object> output = this.decoder Flux<Object> output = this.decoder.decode(source, ResolvableType.forClass(TypePojo.class),
.decode(source, ResolvableType.forClass(TypePojo.class), null); null, Collections.emptyMap());
TestSubscriber TestSubscriber
.subscribe(output) .subscribe(output)
@@ -186,8 +187,8 @@ public class Jaxb2XmlDecoderTests extends AbstractDataBufferAllocatingTestCase {
@Test @Test
public void decodeMultipleXmlRootElement() throws Exception { public void decodeMultipleXmlRootElement() throws Exception {
Flux<DataBuffer> source = Flux.just(stringBuffer(POJO_CHILD)); Flux<DataBuffer> source = Flux.just(stringBuffer(POJO_CHILD));
Flux<Object> output = Flux<Object> output = this.decoder.decode(source, ResolvableType.forClass(Pojo.class),
this.decoder.decode(source, ResolvableType.forClass(Pojo.class), null); null, Collections.emptyMap());
TestSubscriber TestSubscriber
.subscribe(output) .subscribe(output)
@@ -199,8 +200,8 @@ public class Jaxb2XmlDecoderTests extends AbstractDataBufferAllocatingTestCase {
@Test @Test
public void decodeMultipleXmlTypeElement() throws Exception { public void decodeMultipleXmlTypeElement() throws Exception {
Flux<DataBuffer> source = Flux.just(stringBuffer(POJO_CHILD)); Flux<DataBuffer> source = Flux.just(stringBuffer(POJO_CHILD));
Flux<Object> output = this.decoder Flux<Object> output = this.decoder.decode(source, ResolvableType.forClass(TypePojo.class),
.decode(source, ResolvableType.forClass(TypePojo.class), null); null, Collections.emptyMap());
TestSubscriber TestSubscriber
.subscribe(output) .subscribe(output)

View File

@@ -17,6 +17,7 @@
package org.springframework.http.codec.xml; package org.springframework.http.codec.xml;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Collections;
import org.junit.Test; import org.junit.Test;
import reactor.core.publisher.Flux; import reactor.core.publisher.Flux;
@@ -45,18 +46,18 @@ public class Jaxb2XmlEncoderTests extends AbstractDataBufferAllocatingTestCase {
@Test @Test
public void canEncode() { public void canEncode() {
assertTrue(this.encoder.canEncode(ResolvableType.forClass(Pojo.class), assertTrue(this.encoder.canEncode(ResolvableType.forClass(Pojo.class),
MediaType.APPLICATION_XML)); MediaType.APPLICATION_XML, Collections.emptyMap()));
assertTrue(this.encoder.canEncode(ResolvableType.forClass(Pojo.class), assertTrue(this.encoder.canEncode(ResolvableType.forClass(Pojo.class),
MediaType.TEXT_XML)); MediaType.TEXT_XML, Collections.emptyMap()));
assertFalse(this.encoder.canEncode(ResolvableType.forClass(Pojo.class), assertFalse(this.encoder.canEncode(ResolvableType.forClass(Pojo.class),
MediaType.APPLICATION_JSON)); MediaType.APPLICATION_JSON, Collections.emptyMap()));
assertTrue(this.encoder.canEncode( assertTrue(this.encoder.canEncode(
ResolvableType.forClass(Jaxb2XmlDecoderTests.TypePojo.class), ResolvableType.forClass(Jaxb2XmlDecoderTests.TypePojo.class),
MediaType.APPLICATION_XML)); MediaType.APPLICATION_XML, Collections.emptyMap()));
assertFalse(this.encoder.canEncode(ResolvableType.forClass(getClass()), assertFalse(this.encoder.canEncode(ResolvableType.forClass(getClass()),
MediaType.APPLICATION_XML)); MediaType.APPLICATION_XML, Collections.emptyMap()));
} }
@Test @Test
@@ -64,7 +65,7 @@ public class Jaxb2XmlEncoderTests extends AbstractDataBufferAllocatingTestCase {
Flux<Pojo> source = Flux.just(new Pojo("foofoo", "barbar"), new Pojo("foofoofoo", "barbarbar")); Flux<Pojo> source = Flux.just(new Pojo("foofoo", "barbar"), new Pojo("foofoofoo", "barbarbar"));
Flux<DataBuffer> output = this.encoder.encode(source, this.bufferFactory, Flux<DataBuffer> output = this.encoder.encode(source, this.bufferFactory,
ResolvableType.forClass(Pojo.class), ResolvableType.forClass(Pojo.class),
MediaType.APPLICATION_XML); MediaType.APPLICATION_XML, Collections.emptyMap());
TestSubscriber TestSubscriber
.subscribe(output) .subscribe(output)
.assertValuesWith(dataBuffer -> { .assertValuesWith(dataBuffer -> {

View File

@@ -16,6 +16,7 @@
package org.springframework.http.codec.xml; package org.springframework.http.codec.xml;
import java.util.Collections;
import javax.xml.stream.events.XMLEvent; import javax.xml.stream.events.XMLEvent;
import org.junit.Test; import org.junit.Test;
@@ -44,7 +45,7 @@ public class XmlEventDecoderTests extends AbstractDataBufferAllocatingTestCase {
public void toXMLEventsAalto() { public void toXMLEventsAalto() {
Flux<XMLEvent> events = Flux<XMLEvent> events =
this.decoder.decode(Flux.just(stringBuffer(XML)), null, null); this.decoder.decode(Flux.just(stringBuffer(XML)), null, null, Collections.emptyMap());
TestSubscriber TestSubscriber
.subscribe(events) .subscribe(events)
@@ -66,7 +67,7 @@ public class XmlEventDecoderTests extends AbstractDataBufferAllocatingTestCase {
decoder.useAalto = false; decoder.useAalto = false;
Flux<XMLEvent> events = Flux<XMLEvent> events =
this.decoder.decode(Flux.just(stringBuffer(XML)), null, null); this.decoder.decode(Flux.just(stringBuffer(XML)), null, null, Collections.emptyMap());
TestSubscriber TestSubscriber
.subscribe(events) .subscribe(events)