Add WebFlux support for Smile streaming

The commit brings following changes:
 - Move getDecodableMimeTypes() to AbstractJackson2Decoder
 - Move getEncodableMimeTypes() to AbstractJackson2Encoder
 - Add support for application/stream+x-jackson-smile
 - Avoid streaming line separator when Smile encoder is used
 - Use double null token in Jackson2Tokenizer to identify documents

Issue: SPR-16151
This commit is contained in:
sdeleuze
2018-01-29 11:53:27 +01:00
parent e1fa65a37c
commit 32f6ccece8
12 changed files with 112 additions and 50 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -52,6 +52,8 @@ import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.springframework.core.ResolvableType.forClass;
import static org.springframework.http.MediaType.APPLICATION_JSON;
import static org.springframework.http.MediaType.APPLICATION_JSON_UTF8;
import static org.springframework.http.MediaType.APPLICATION_STREAM_JSON;
import static org.springframework.http.MediaType.APPLICATION_XML;
import static org.springframework.http.codec.json.Jackson2JsonDecoder.JSON_VIEW_HINT;
import static org.springframework.http.codec.json.JacksonViewBean.MyJacksonView1;
@@ -70,6 +72,8 @@ public class Jackson2JsonDecoderTests extends AbstractDataBufferAllocatingTestCa
Jackson2JsonDecoder decoder = new Jackson2JsonDecoder();
assertTrue(decoder.canDecode(forClass(Pojo.class), APPLICATION_JSON));
assertTrue(decoder.canDecode(forClass(Pojo.class), APPLICATION_JSON_UTF8));
assertTrue(decoder.canDecode(forClass(Pojo.class), APPLICATION_STREAM_JSON));
assertTrue(decoder.canDecode(forClass(Pojo.class), null));
assertFalse(decoder.canDecode(forClass(String.class), null));
@@ -130,7 +134,7 @@ public class Jackson2JsonDecoderTests extends AbstractDataBufferAllocatingTestCa
}
@Test
public void decodeToFlux() throws Exception {
public void decodeArrayToFlux() throws Exception {
Flux<DataBuffer> source = Flux.just(stringBuffer(
"[{\"bar\":\"b1\",\"foo\":\"f1\"},{\"bar\":\"b2\",\"foo\":\"f2\"}]"));
@@ -144,6 +148,21 @@ public class Jackson2JsonDecoderTests extends AbstractDataBufferAllocatingTestCa
.verifyComplete();
}
@Test
public void decodeStreamToFlux() throws Exception {
Flux<DataBuffer> source = Flux.just(stringBuffer("{\"bar\":\"b1\",\"foo\":\"f1\"}"),
stringBuffer("{\"bar\":\"b2\",\"foo\":\"f2\"}"));
ResolvableType elementType = forClass(Pojo.class);
Flux<Object> flux = new Jackson2JsonDecoder().decode(source, elementType, APPLICATION_STREAM_JSON,
emptyMap());
StepVerifier.create(flux)
.expectNext(new Pojo("f1", "b1"))
.expectNext(new Pojo("f2", "b2"))
.verifyComplete();
}
@Test
public void decodeEmptyArrayToFlux() throws Exception {
Flux<DataBuffer> source = Flux.just(stringBuffer("[]"));

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -57,6 +57,8 @@ public class Jackson2JsonEncoderTests extends AbstractDataBufferAllocatingTestCa
public void canEncode() {
ResolvableType pojoType = ResolvableType.forClass(Pojo.class);
assertTrue(this.encoder.canEncode(pojoType, APPLICATION_JSON));
assertTrue(this.encoder.canEncode(pojoType, APPLICATION_JSON_UTF8));
assertTrue(this.encoder.canEncode(pojoType, APPLICATION_STREAM_JSON));
assertTrue(this.encoder.canEncode(pojoType, null));
// SPR-15464

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -38,6 +38,7 @@ import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.springframework.core.ResolvableType.forClass;
import static org.springframework.http.MediaType.APPLICATION_JSON;
import static org.springframework.http.MediaType.APPLICATION_STREAM_JSON;
/**
* Unit tests for {@link Jackson2SmileDecoder}.
@@ -47,12 +48,14 @@ import static org.springframework.http.MediaType.APPLICATION_JSON;
public class Jackson2SmileDecoderTests extends AbstractDataBufferAllocatingTestCase {
private final static MimeType SMILE_MIME_TYPE = new MimeType("application", "x-jackson-smile");
private final static MimeType STREAM_SMILE_MIME_TYPE = new MimeType("application", "stream+x-jackson-smile");
private final Jackson2SmileDecoder decoder = new Jackson2SmileDecoder();
@Test
public void canDecode() {
assertTrue(decoder.canDecode(forClass(Pojo.class), SMILE_MIME_TYPE));
assertTrue(decoder.canDecode(forClass(Pojo.class), STREAM_SMILE_MIME_TYPE));
assertTrue(decoder.canDecode(forClass(Pojo.class), null));
assertFalse(decoder.canDecode(forClass(String.class), null));
@@ -100,7 +103,7 @@ public class Jackson2SmileDecoderTests extends AbstractDataBufferAllocatingTestC
}
@Test
public void decodeToFlux() throws Exception {
public void decodeListToFlux() throws Exception {
ObjectMapper mapper = Jackson2ObjectMapperBuilder.smile().build();
List<Pojo> list = asList(new Pojo("f1", "b1"), new Pojo("f2", "b2"));
byte[] serializedList = mapper.writer().writeValueAsBytes(list);
@@ -115,4 +118,20 @@ public class Jackson2SmileDecoderTests extends AbstractDataBufferAllocatingTestC
.verifyComplete();
}
@Test
public void decodeStreamToFlux() throws Exception {
ObjectMapper mapper = Jackson2ObjectMapperBuilder.smile().build();
List<Pojo> list = asList(new Pojo("f1", "b1"), new Pojo("f2", "b2"));
byte[] serializedList = mapper.writer().writeValueAsBytes(list);
Flux<DataBuffer> source = Flux.just(this.bufferFactory.wrap(serializedList));
ResolvableType elementType = forClass(Pojo.class);
Flux<Object> flux = decoder.decode(source, elementType, STREAM_SMILE_MIME_TYPE, emptyMap());
StepVerifier.create(flux)
.expectNext(new Pojo("f1", "b1"))
.expectNext(new Pojo("f2", "b2"))
.verifyComplete();
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -49,6 +49,7 @@ import static org.springframework.http.MediaType.APPLICATION_XML;
public class Jackson2SmileEncoderTests extends AbstractDataBufferAllocatingTestCase {
private final static MimeType SMILE_MIME_TYPE = new MimeType("application", "x-jackson-smile");
private final static MimeType STREAM_SMILE_MIME_TYPE = new MimeType("application", "stream+x-jackson-smile");
private final Jackson2SmileEncoder encoder = new Jackson2SmileEncoder();
@@ -57,6 +58,7 @@ public class Jackson2SmileEncoderTests extends AbstractDataBufferAllocatingTestC
public void canEncode() {
ResolvableType pojoType = ResolvableType.forClass(Pojo.class);
assertTrue(this.encoder.canEncode(pojoType, SMILE_MIME_TYPE));
assertTrue(this.encoder.canEncode(pojoType, STREAM_SMILE_MIME_TYPE));
assertTrue(this.encoder.canEncode(pojoType, null));
// SPR-15464
@@ -96,8 +98,7 @@ public class Jackson2SmileEncoderTests extends AbstractDataBufferAllocatingTestC
new Pojo("foofoofoo", "barbarbar")
);
ResolvableType type = ResolvableType.forClass(Pojo.class);
MediaType mediaType = new MediaType("application", "stream+x-jackson-smile");
Flux<DataBuffer> output = this.encoder.encode(source, this.bufferFactory, type, mediaType, emptyMap());
Flux<DataBuffer> output = this.encoder.encode(source, this.bufferFactory, type, STREAM_SMILE_MIME_TYPE, emptyMap());
ObjectMapper mapper = Jackson2ObjectMapperBuilder.smile().build();
StepVerifier.create(output)