Search in sources :

Example 66 with DataBuffer

use of org.springframework.core.io.buffer.DataBuffer in project spring-framework by spring-projects.

the class Jackson2JsonEncoderTests method jsonView.

@Test
public void jsonView() throws Exception {
    JacksonViewBean bean = new JacksonViewBean();
    bean.setWithView1("with");
    bean.setWithView2("with");
    bean.setWithoutView("without");
    ResolvableType type = ResolvableType.forClass(JacksonViewBean.class);
    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);
    StepVerifier.create(output).consumeNextWith(stringConsumer("{\"withView1\":\"with\"}")).expectComplete().verify();
}
Also used : ResolvableType(org.springframework.core.ResolvableType) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.Test)

Example 67 with DataBuffer

use of org.springframework.core.io.buffer.DataBuffer in project spring-framework by spring-projects.

the class Jaxb2XmlDecoderTests method decodeSingleXmlRootElement.

@Test
public void decodeSingleXmlRootElement() throws Exception {
    Flux<DataBuffer> source = Flux.just(stringBuffer(POJO_ROOT));
    Flux<Object> output = this.decoder.decode(source, ResolvableType.forClass(Pojo.class), null, Collections.emptyMap());
    StepVerifier.create(output).expectNext(new Pojo("foofoo", "barbar")).expectComplete().verify();
}
Also used : Pojo(org.springframework.http.codec.Pojo) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.Test)

Example 68 with DataBuffer

use of org.springframework.core.io.buffer.DataBuffer in project spring-framework by spring-projects.

the class Jaxb2XmlDecoderTests method decodeMultipleXmlRootElement.

@Test
public void decodeMultipleXmlRootElement() throws Exception {
    Flux<DataBuffer> source = Flux.just(stringBuffer(POJO_CHILD));
    Flux<Object> output = this.decoder.decode(source, ResolvableType.forClass(Pojo.class), null, Collections.emptyMap());
    StepVerifier.create(output).expectNext(new Pojo("foo", "bar")).expectNext(new Pojo("foofoo", "barbar")).expectComplete().verify();
}
Also used : Pojo(org.springframework.http.codec.Pojo) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.Test)

Example 69 with DataBuffer

use of org.springframework.core.io.buffer.DataBuffer in project spring-framework by spring-projects.

the class Jaxb2XmlEncoderTests method encode.

@Test
public void encode() throws Exception {
    Flux<Pojo> source = Flux.just(new Pojo("foofoo", "barbar"), new Pojo("foofoofoo", "barbarbar"));
    Flux<DataBuffer> output = this.encoder.encode(source, this.bufferFactory, ResolvableType.forClass(Pojo.class), MediaType.APPLICATION_XML, Collections.emptyMap());
    StepVerifier.create(output).consumeNextWith(dataBuffer -> {
        try {
            String s = DataBufferTestUtils.dumpString(dataBuffer, StandardCharsets.UTF_8);
            assertThat(s, isSimilarTo("<?xml version='1.0' encoding='UTF-8' standalone='yes'?>" + "<pojo><bar>barbar</bar><foo>foofoo</foo></pojo>"));
        } finally {
            DataBufferUtils.release(dataBuffer);
        }
    }).expectComplete().verify();
}
Also used : Pojo(org.springframework.http.codec.Pojo) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.Test)

Example 70 with DataBuffer

use of org.springframework.core.io.buffer.DataBuffer in project spring-framework by spring-projects.

the class Jackson2JsonDecoderTests method jsonView.

@Test
public void jsonView() throws Exception {
    Flux<DataBuffer> source = Flux.just(stringBuffer("{\"withView1\" : \"with\", \"withView2\" : \"with\", \"withoutView\" : \"without\"}"));
    ResolvableType elementType = ResolvableType.forClass(JacksonViewBean.class);
    Map<String, Object> hints = Collections.singletonMap(Jackson2JsonDecoder.JSON_VIEW_HINT, MyJacksonView1.class);
    Flux<JacksonViewBean> flux = new Jackson2JsonDecoder().decode(source, elementType, null, hints).cast(JacksonViewBean.class);
    StepVerifier.create(flux).consumeNextWith(b -> {
        assertTrue(b.getWithView1().equals("with"));
        assertNull(b.getWithView2());
        assertNull(b.getWithoutView());
    }).expectComplete().verify();
}
Also used : ResolvableType(org.springframework.core.ResolvableType) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.Test)

Aggregations

DataBuffer (org.springframework.core.io.buffer.DataBuffer)70 Test (org.junit.Test)44 DefaultDataBufferFactory (org.springframework.core.io.buffer.DefaultDataBufferFactory)21 DefaultDataBuffer (org.springframework.core.io.buffer.DefaultDataBuffer)17 ResolvableType (org.springframework.core.ResolvableType)15 ByteBuffer (java.nio.ByteBuffer)11 Flux (reactor.core.publisher.Flux)11 Mono (reactor.core.publisher.Mono)11 List (java.util.List)9 Publisher (org.reactivestreams.Publisher)9 HttpHeaders (org.springframework.http.HttpHeaders)9 MediaType (org.springframework.http.MediaType)8 DecoderHttpMessageReader (org.springframework.http.codec.DecoderHttpMessageReader)8 HttpMessageReader (org.springframework.http.codec.HttpMessageReader)8 Pojo (org.springframework.http.codec.Pojo)8 MockServerHttpRequest (org.springframework.mock.http.server.reactive.test.MockServerHttpRequest)8 StringDecoder (org.springframework.core.codec.StringDecoder)7 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)6 ReactiveHttpOutputMessage (org.springframework.http.ReactiveHttpOutputMessage)6