Search in sources :

Example 16 with Jackson2JsonEncoder

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

the class ViewResolverRegistryTests method defaultViews.

@Test
public void defaultViews() throws Exception {
    View view = new HttpMessageWriterView(new Jackson2JsonEncoder());
    this.registry.defaultViews(view);
    assertThat(this.registry.getDefaultViews().size()).isEqualTo(1);
    assertThat(this.registry.getDefaultViews().get(0)).isSameAs(view);
}
Also used : HttpMessageWriterView(org.springframework.web.reactive.result.view.HttpMessageWriterView) HttpMessageWriterView(org.springframework.web.reactive.result.view.HttpMessageWriterView) View(org.springframework.web.reactive.result.view.View) Jackson2JsonEncoder(org.springframework.http.codec.json.Jackson2JsonEncoder) Test(org.junit.jupiter.api.Test)

Example 17 with Jackson2JsonEncoder

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

the class CancelWithoutDemandCodecTests method cancelWithJackson.

// gh-22107
@Test
public void cancelWithJackson() {
    Jackson2JsonEncoder encoder = new Jackson2JsonEncoder();
    Flux<DataBuffer> flux = encoder.encode(Flux.just(new Pojo("foofoo", "barbar"), new Pojo("bar", "baz")), this.bufferFactory, ResolvableType.forClass(Pojo.class), MediaType.APPLICATION_JSON, Collections.emptyMap());
    BaseSubscriber<DataBuffer> subscriber = new ZeroDemandSubscriber();
    // Assume sync execution (e.g. encoding with Flux.just)..
    flux.subscribe(subscriber);
    subscriber.cancel();
}
Also used : Pojo(org.springframework.web.testfixture.xml.Pojo) Jackson2JsonEncoder(org.springframework.http.codec.json.Jackson2JsonEncoder) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test)

Example 18 with Jackson2JsonEncoder

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

the class ResponseEntityResultHandlerTests method createHandler.

private ResponseEntityResultHandler createHandler(HttpMessageWriter<?>... writers) {
    List<HttpMessageWriter<?>> writerList;
    if (ObjectUtils.isEmpty(writers)) {
        writerList = new ArrayList<>();
        writerList.add(new EncoderHttpMessageWriter<>(new ByteBufferEncoder()));
        writerList.add(new EncoderHttpMessageWriter<>(CharSequenceEncoder.textPlainOnly()));
        writerList.add(new ResourceHttpMessageWriter());
        writerList.add(new EncoderHttpMessageWriter<>(new Jaxb2XmlEncoder()));
        writerList.add(new EncoderHttpMessageWriter<>(new Jackson2JsonEncoder()));
        writerList.add(new EncoderHttpMessageWriter<>(CharSequenceEncoder.allMimeTypes()));
    } else {
        writerList = Arrays.asList(writers);
    }
    RequestedContentTypeResolver resolver = new RequestedContentTypeResolverBuilder().build();
    return new ResponseEntityResultHandler(writerList, resolver);
}
Also used : ByteBufferEncoder(org.springframework.core.codec.ByteBufferEncoder) ResourceHttpMessageWriter(org.springframework.http.codec.ResourceHttpMessageWriter) EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) HttpMessageWriter(org.springframework.http.codec.HttpMessageWriter) ResourceHttpMessageWriter(org.springframework.http.codec.ResourceHttpMessageWriter) RequestedContentTypeResolver(org.springframework.web.reactive.accept.RequestedContentTypeResolver) Jaxb2XmlEncoder(org.springframework.http.codec.xml.Jaxb2XmlEncoder) Jackson2JsonEncoder(org.springframework.http.codec.json.Jackson2JsonEncoder) RequestedContentTypeResolverBuilder(org.springframework.web.reactive.accept.RequestedContentTypeResolverBuilder)

Example 19 with Jackson2JsonEncoder

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

the class ServerSentEventHttpMessageWriterTests method writePojoWithPrettyPrint.

// SPR-14899
@ParameterizedDataBufferAllocatingTest
void writePojoWithPrettyPrint(String displayName, DataBufferFactory bufferFactory) {
    super.bufferFactory = bufferFactory;
    ObjectMapper mapper = Jackson2ObjectMapperBuilder.json().indentOutput(true).build();
    this.messageWriter = new ServerSentEventHttpMessageWriter(new Jackson2JsonEncoder(mapper));
    MockServerHttpResponse outputMessage = new MockServerHttpResponse(super.bufferFactory);
    Flux<Pojo> source = Flux.just(new Pojo("foofoo", "barbar"), new Pojo("foofoofoo", "barbarbar"));
    testWrite(source, outputMessage, Pojo.class);
    StepVerifier.create(outputMessage.getBody()).consumeNextWith(stringConsumer("data:")).consumeNextWith(stringConsumer("{\n" + "data:  \"foo\" : \"foofoo\",\n" + "data:  \"bar\" : \"barbar\"\n" + "data:}")).consumeNextWith(stringConsumer("\n\n")).consumeNextWith(stringConsumer("data:")).consumeNextWith(stringConsumer("{\n" + "data:  \"foo\" : \"foofoofoo\",\n" + "data:  \"bar\" : \"barbarbar\"\n" + "data:}")).consumeNextWith(stringConsumer("\n\n")).expectComplete().verify();
}
Also used : Pojo(org.springframework.web.testfixture.xml.Pojo) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Jackson2JsonEncoder(org.springframework.http.codec.json.Jackson2JsonEncoder)

Example 20 with Jackson2JsonEncoder

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

the class CodecConfigurerTests method encoderDecoderOverrides.

@Test
void encoderDecoderOverrides() {
    Jackson2JsonDecoder jacksonDecoder = new Jackson2JsonDecoder();
    Jackson2JsonEncoder jacksonEncoder = new Jackson2JsonEncoder();
    Jackson2SmileDecoder smileDecoder = new Jackson2SmileDecoder();
    Jackson2SmileEncoder smileEncoder = new Jackson2SmileEncoder();
    ProtobufDecoder protobufDecoder = new ProtobufDecoder(ExtensionRegistry.newInstance());
    ProtobufEncoder protobufEncoder = new ProtobufEncoder();
    Jaxb2XmlEncoder jaxb2Encoder = new Jaxb2XmlEncoder();
    Jaxb2XmlDecoder jaxb2Decoder = new Jaxb2XmlDecoder();
    this.configurer.defaultCodecs().jackson2JsonDecoder(jacksonDecoder);
    this.configurer.defaultCodecs().jackson2JsonEncoder(jacksonEncoder);
    this.configurer.defaultCodecs().jackson2SmileDecoder(smileDecoder);
    this.configurer.defaultCodecs().jackson2SmileEncoder(smileEncoder);
    this.configurer.defaultCodecs().protobufDecoder(protobufDecoder);
    this.configurer.defaultCodecs().protobufEncoder(protobufEncoder);
    this.configurer.defaultCodecs().jaxb2Decoder(jaxb2Decoder);
    this.configurer.defaultCodecs().jaxb2Encoder(jaxb2Encoder);
    assertDecoderInstance(jacksonDecoder);
    assertDecoderInstance(smileDecoder);
    assertDecoderInstance(protobufDecoder);
    assertDecoderInstance(jaxb2Decoder);
    assertEncoderInstance(jacksonEncoder);
    assertEncoderInstance(smileEncoder);
    assertEncoderInstance(protobufEncoder);
    assertEncoderInstance(jaxb2Encoder);
}
Also used : ProtobufEncoder(org.springframework.http.codec.protobuf.ProtobufEncoder) Jackson2JsonDecoder(org.springframework.http.codec.json.Jackson2JsonDecoder) Jackson2SmileEncoder(org.springframework.http.codec.json.Jackson2SmileEncoder) ProtobufDecoder(org.springframework.http.codec.protobuf.ProtobufDecoder) Jaxb2XmlDecoder(org.springframework.http.codec.xml.Jaxb2XmlDecoder) Jaxb2XmlEncoder(org.springframework.http.codec.xml.Jaxb2XmlEncoder) Jackson2JsonEncoder(org.springframework.http.codec.json.Jackson2JsonEncoder) Jackson2SmileDecoder(org.springframework.http.codec.json.Jackson2SmileDecoder) Test(org.junit.jupiter.api.Test)

Aggregations

Jackson2JsonEncoder (org.springframework.http.codec.json.Jackson2JsonEncoder)24 Test (org.junit.jupiter.api.Test)14 EncoderHttpMessageWriter (org.springframework.http.codec.EncoderHttpMessageWriter)13 ResourceHttpMessageWriter (org.springframework.http.codec.ResourceHttpMessageWriter)13 HttpMessageWriter (org.springframework.http.codec.HttpMessageWriter)11 ByteBufferEncoder (org.springframework.core.codec.ByteBufferEncoder)9 ServerSentEventHttpMessageWriter (org.springframework.http.codec.ServerSentEventHttpMessageWriter)9 Jaxb2XmlEncoder (org.springframework.http.codec.xml.Jaxb2XmlEncoder)8 FormHttpMessageWriter (org.springframework.http.codec.FormHttpMessageWriter)7 Jackson2JsonDecoder (org.springframework.http.codec.json.Jackson2JsonDecoder)7 MultipartHttpMessageWriter (org.springframework.http.codec.multipart.MultipartHttpMessageWriter)6 ArrayList (java.util.ArrayList)5 ServerSentEventHttpMessageReader (org.springframework.http.codec.ServerSentEventHttpMessageReader)5 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)4 ProtobufHttpMessageWriter (org.springframework.http.codec.protobuf.ProtobufHttpMessageWriter)4 HashMap (java.util.HashMap)3 ByteArrayEncoder (org.springframework.core.codec.ByteArrayEncoder)3 CharSequenceEncoder (org.springframework.core.codec.CharSequenceEncoder)3 CodecConfigurer (org.springframework.http.codec.CodecConfigurer)3 DecoderHttpMessageReader (org.springframework.http.codec.DecoderHttpMessageReader)3