Search in sources :

Example 91 with ResolvableType

use of org.springframework.core.ResolvableType in project spring-framework by spring-projects.

the class Jackson2JsonEncoderTests method canEncode.

@Test
public void canEncode() {
    ResolvableType pojoType = ResolvableType.forClass(Pojo.class);
    assertTrue(this.encoder.canEncode(pojoType, MediaType.APPLICATION_JSON));
    assertTrue(this.encoder.canEncode(pojoType, null));
    assertFalse(this.encoder.canEncode(pojoType, MediaType.APPLICATION_XML));
    ResolvableType sseType = ResolvableType.forClass(ServerSentEvent.class);
    assertFalse(this.encoder.canEncode(sseType, MediaType.APPLICATION_JSON));
}
Also used : ResolvableType(org.springframework.core.ResolvableType) Test(org.junit.Test)

Example 92 with ResolvableType

use of org.springframework.core.ResolvableType 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)

Example 93 with ResolvableType

use of org.springframework.core.ResolvableType 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 94 with ResolvableType

use of org.springframework.core.ResolvableType in project spring-framework by spring-projects.

the class Jackson2JsonDecoderTests method canDecode.

@Test
public void canDecode() {
    Jackson2JsonDecoder decoder = new Jackson2JsonDecoder();
    ResolvableType type = ResolvableType.forClass(Pojo.class);
    assertTrue(decoder.canDecode(type, MediaType.APPLICATION_JSON));
    assertTrue(decoder.canDecode(type, null));
    assertFalse(decoder.canDecode(type, MediaType.APPLICATION_XML));
}
Also used : ResolvableType(org.springframework.core.ResolvableType) Test(org.junit.Test)

Example 95 with ResolvableType

use of org.springframework.core.ResolvableType 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

ResolvableType (org.springframework.core.ResolvableType)96 Test (org.junit.Test)66 MethodParameter (org.springframework.core.MethodParameter)20 DataBuffer (org.springframework.core.io.buffer.DataBuffer)15 ServerWebExchange (org.springframework.web.server.ServerWebExchange)14 List (java.util.List)11 MediaType (org.springframework.http.MediaType)10 Flux (reactor.core.publisher.Flux)10 Mono (reactor.core.publisher.Mono)10 Map (java.util.Map)9 Assert (org.springframework.util.Assert)7 Collections (java.util.Collections)6 MockServerWebExchange (org.springframework.mock.http.server.reactive.test.MockServerWebExchange)6 ArrayList (java.util.ArrayList)5 ReactiveAdapter (org.springframework.core.ReactiveAdapter)5 Pojo (org.springframework.http.codec.Pojo)5 ServerWebInputException (org.springframework.web.server.ServerWebInputException)5 Publisher (org.reactivestreams.Publisher)4 HttpMessageReader (org.springframework.http.codec.HttpMessageReader)4 Single (rx.Single)4