Search in sources :

Example 1 with Pojo

use of org.springframework.http.codec.Pojo 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 2 with Pojo

use of org.springframework.http.codec.Pojo 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 3 with Pojo

use of org.springframework.http.codec.Pojo in project spring-framework by spring-projects.

the class WebClientIntegrationTests method postJsonPojo.

@Test
public void postJsonPojo() throws Exception {
    this.server.enqueue(new MockResponse().setHeader("Content-Type", "application/json").setBody("{\"bar\":\"BARBAR\",\"foo\":\"FOOFOO\"}"));
    Mono<Pojo> result = this.webClient.post().uri("/pojo/capitalize").accept(MediaType.APPLICATION_JSON).contentType(MediaType.APPLICATION_JSON).exchange(fromObject(new Pojo("foofoo", "barbar"))).then(response -> response.bodyToMono(Pojo.class));
    StepVerifier.create(result).consumeNextWith(p -> assertEquals("BARBAR", p.getBar())).expectComplete().verify(Duration.ofSeconds(3));
    RecordedRequest recordedRequest = server.takeRequest();
    Assert.assertEquals(1, server.getRequestCount());
    Assert.assertEquals("/pojo/capitalize", recordedRequest.getPath());
    Assert.assertEquals("{\"foo\":\"foofoo\",\"bar\":\"barbar\"}", recordedRequest.getBody().readUtf8());
    Assert.assertEquals("chunked", recordedRequest.getHeader(HttpHeaders.TRANSFER_ENCODING));
    Assert.assertEquals("application/json", recordedRequest.getHeader(HttpHeaders.ACCEPT));
    Assert.assertEquals("application/json", recordedRequest.getHeader(HttpHeaders.CONTENT_TYPE));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) Pojo(org.springframework.http.codec.Pojo) Test(org.junit.Test)

Example 4 with Pojo

use of org.springframework.http.codec.Pojo in project spring-framework by spring-projects.

the class Jackson2JsonDecoderTests method decodeToFlux.

@Test
public void decodeToFlux() throws Exception {
    Flux<DataBuffer> source = Flux.just(stringBuffer("[{\"bar\":\"b1\",\"foo\":\"f1\"},{\"bar\":\"b2\",\"foo\":\"f2\"}]"));
    ResolvableType elementType = ResolvableType.forClass(Pojo.class);
    Flux<Object> flux = new Jackson2JsonDecoder().decode(source, elementType, null, Collections.emptyMap());
    StepVerifier.create(flux).expectNext(new Pojo("f1", "b1")).expectNext(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 5 with Pojo

use of org.springframework.http.codec.Pojo in project spring-framework by spring-projects.

the class Jackson2JsonEncoderTests method encodeAsStream.

@Test
public void encodeAsStream() throws Exception {
    Flux<Pojo> source = Flux.just(new Pojo("foo", "bar"), new Pojo("foofoo", "barbar"), new Pojo("foofoofoo", "barbarbar"));
    ResolvableType type = ResolvableType.forClass(Pojo.class);
    Flux<DataBuffer> output = this.encoder.encode(source, this.bufferFactory, type, APPLICATION_STREAM_JSON, Collections.emptyMap());
    StepVerifier.create(output).consumeNextWith(stringConsumer("{\"foo\":\"foo\",\"bar\":\"bar\"}\n")).consumeNextWith(stringConsumer("{\"foo\":\"foofoo\",\"bar\":\"barbar\"}\n")).consumeNextWith(stringConsumer("{\"foo\":\"foofoofoo\",\"bar\":\"barbarbar\"}\n")).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)

Aggregations

Test (org.junit.Test)11 Pojo (org.springframework.http.codec.Pojo)11 DataBuffer (org.springframework.core.io.buffer.DataBuffer)7 ResolvableType (org.springframework.core.ResolvableType)5 MockResponse (okhttp3.mockwebserver.MockResponse)3 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)3 QName (javax.xml.namespace.QName)2 MediaType (org.springframework.http.MediaType)2 XmlRootElement (org.springframework.http.codec.xml.jaxb.XmlRootElement)2 XmlRootElementWithName (org.springframework.http.codec.xml.jaxb.XmlRootElementWithName)2 XmlRootElementWithNameAndNamespace (org.springframework.http.codec.xml.jaxb.XmlRootElementWithNameAndNamespace)2 XmlType (org.springframework.http.codec.xml.jaxb.XmlType)2 XmlTypeWithName (org.springframework.http.codec.xml.jaxb.XmlTypeWithName)2 XmlTypeWithNameAndNamespace (org.springframework.http.codec.xml.jaxb.XmlTypeWithNameAndNamespace)2 Flux (reactor.core.publisher.Flux)2 StepVerifier (reactor.test.StepVerifier)2 Duration (java.time.Duration)1 Collections (java.util.Collections)1 List (java.util.List)1 XMLEvent (javax.xml.stream.events.XMLEvent)1