Search in sources :

Example 1 with DataBuffer

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

the class Jaxb2XmlEncoder method encode.

@Override
protected Flux<DataBuffer> encode(Object value, DataBufferFactory dataBufferFactory, ResolvableType type, MimeType mimeType, Map<String, Object> hints) {
    try {
        DataBuffer buffer = dataBufferFactory.allocateBuffer(1024);
        OutputStream outputStream = buffer.asOutputStream();
        Class<?> clazz = ClassUtils.getUserClass(value);
        Marshaller marshaller = jaxbContexts.createMarshaller(clazz);
        marshaller.setProperty(Marshaller.JAXB_ENCODING, StandardCharsets.UTF_8.name());
        marshaller.marshal(value, outputStream);
        return Flux.just(buffer);
    } catch (JAXBException ex) {
        return Flux.error(ex);
    }
}
Also used : Marshaller(javax.xml.bind.Marshaller) OutputStream(java.io.OutputStream) JAXBException(javax.xml.bind.JAXBException) DataBuffer(org.springframework.core.io.buffer.DataBuffer)

Example 2 with DataBuffer

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

the class Jackson2JsonDecoderTests method decodeToList.

@Test
public void decodeToList() throws Exception {
    Flux<DataBuffer> source = Flux.just(stringBuffer("[{\"bar\":\"b1\",\"foo\":\"f1\"},{\"bar\":\"b2\",\"foo\":\"f2\"}]"));
    ResolvableType elementType = ResolvableType.forClassWithGenerics(List.class, Pojo.class);
    Mono<Object> mono = new Jackson2JsonDecoder().decodeToMono(source, elementType, null, Collections.emptyMap());
    StepVerifier.create(mono).expectNext(Arrays.asList(new Pojo("f1", "b1"), new Pojo("f2", "b2"))).expectComplete().verify();
}
Also used : Pojo(org.springframework.http.codec.Pojo) ResolvableType(org.springframework.core.ResolvableType) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.Test)

Example 3 with DataBuffer

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

the class Jackson2JsonDecoderTests method decodePojo.

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

Example 4 with DataBuffer

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

the class Jackson2JsonDecoderTests method decodeEmptyBodyToMono.

@Test
public void decodeEmptyBodyToMono() throws Exception {
    Flux<DataBuffer> source = Flux.empty();
    ResolvableType elementType = ResolvableType.forClass(Pojo.class);
    Mono<Object> mono = new Jackson2JsonDecoder().decodeToMono(source, elementType, null, Collections.emptyMap());
    StepVerifier.create(mono).expectNextCount(0).expectComplete().verify();
}
Also used : ResolvableType(org.springframework.core.ResolvableType) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.Test)

Example 5 with DataBuffer

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

the class Jackson2JsonEncoderTests method encodeWithType.

@Test
public void encodeWithType() throws Exception {
    Flux<ParentClass> source = Flux.just(new Foo(), new Bar());
    ResolvableType type = ResolvableType.forClass(ParentClass.class);
    Flux<DataBuffer> output = this.encoder.encode(source, this.bufferFactory, type, null, Collections.emptyMap());
    StepVerifier.create(output).consumeNextWith(stringConsumer("[{\"type\":\"foo\"},{\"type\":\"bar\"}]")).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)230 Test (org.junit.jupiter.api.Test)111 Mono (reactor.core.publisher.Mono)55 Flux (reactor.core.publisher.Flux)52 DefaultDataBuffer (org.springframework.core.io.buffer.DefaultDataBuffer)49 ResolvableType (org.springframework.core.ResolvableType)41 DefaultDataBufferFactory (org.springframework.core.io.buffer.DefaultDataBufferFactory)36 StepVerifier (reactor.test.StepVerifier)36 List (java.util.List)34 Test (org.junit.Test)30 DataBufferUtils (org.springframework.core.io.buffer.DataBufferUtils)29 IOException (java.io.IOException)28 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)27 MockServerHttpRequest (org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest)27 Map (java.util.Map)25 NettyDataBuffer (org.springframework.core.io.buffer.NettyDataBuffer)25 DataBufferFactory (org.springframework.core.io.buffer.DataBufferFactory)24 HttpHeaders (org.springframework.http.HttpHeaders)24 MediaType (org.springframework.http.MediaType)24 ByteBuffer (java.nio.ByteBuffer)23