Search in sources :

Example 61 with ResolvableType

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

the class Jackson2JsonEncoderTests method encode.

@Test
public void encode() 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, null, Collections.emptyMap());
    StepVerifier.create(output).consumeNextWith(stringConsumer("[{\"foo\":\"foo\",\"bar\":\"bar\"},{\"foo\":\"foofoo\",\"bar\":\"barbar\"},{\"foo\":\"foofoofoo\",\"bar\":\"barbarbar\"}]")).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 62 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 63 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 64 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 65 with ResolvableType

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

the class JsonComponentModule method addDeserializerWithDeducedType.

@SuppressWarnings({ "unchecked" })
private <T> void addDeserializerWithDeducedType(JsonDeserializer<T> deserializer) {
    ResolvableType type = ResolvableType.forClass(JsonDeserializer.class, deserializer.getClass());
    addDeserializer((Class<T>) type.resolveGeneric(), deserializer);
}
Also used : ResolvableType(org.springframework.core.ResolvableType)

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