Search in sources :

Example 21 with Jackson2JsonEncoder

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

the class CodecConfigurerTests method cloneEmptyCustomCodecs.

@Test
void cloneEmptyCustomCodecs() {
    this.configurer.registerDefaults(false);
    assertThat(this.configurer.getReaders()).isEmpty();
    assertThat(this.configurer.getWriters()).isEmpty();
    CodecConfigurer clone = this.configurer.clone();
    clone.customCodecs().register(new Jackson2JsonEncoder());
    clone.customCodecs().register(new Jackson2JsonDecoder());
    clone.customCodecs().register(new ServerSentEventHttpMessageReader());
    clone.customCodecs().register(new ServerSentEventHttpMessageWriter());
    assertThat(this.configurer.getReaders()).isEmpty();
    assertThat(this.configurer.getWriters()).isEmpty();
    assertThat(clone.getReaders()).hasSize(2);
    assertThat(clone.getWriters()).hasSize(2);
}
Also used : ServerSentEventHttpMessageReader(org.springframework.http.codec.ServerSentEventHttpMessageReader) Jackson2JsonDecoder(org.springframework.http.codec.json.Jackson2JsonDecoder) ServerSentEventHttpMessageWriter(org.springframework.http.codec.ServerSentEventHttpMessageWriter) CodecConfigurer(org.springframework.http.codec.CodecConfigurer) Jackson2JsonEncoder(org.springframework.http.codec.json.Jackson2JsonEncoder) Test(org.junit.jupiter.api.Test)

Example 22 with Jackson2JsonEncoder

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

the class CodecConfigurerTests method cloneCustomCodecs.

@Test
void cloneCustomCodecs() {
    this.configurer.registerDefaults(false);
    assertThat(this.configurer.getReaders()).isEmpty();
    assertThat(this.configurer.getWriters()).isEmpty();
    this.configurer.customCodecs().register(new Jackson2JsonEncoder());
    this.configurer.customCodecs().register(new Jackson2JsonDecoder());
    this.configurer.customCodecs().register(new ServerSentEventHttpMessageReader());
    this.configurer.customCodecs().register(new ServerSentEventHttpMessageWriter());
    assertThat(this.configurer.getReaders()).hasSize(2);
    assertThat(this.configurer.getWriters()).hasSize(2);
    CodecConfigurer clone = this.configurer.clone();
    assertThat(this.configurer.getReaders()).hasSize(2);
    assertThat(this.configurer.getWriters()).hasSize(2);
    assertThat(clone.getReaders()).hasSize(2);
    assertThat(clone.getWriters()).hasSize(2);
}
Also used : ServerSentEventHttpMessageReader(org.springframework.http.codec.ServerSentEventHttpMessageReader) Jackson2JsonDecoder(org.springframework.http.codec.json.Jackson2JsonDecoder) ServerSentEventHttpMessageWriter(org.springframework.http.codec.ServerSentEventHttpMessageWriter) Jackson2JsonEncoder(org.springframework.http.codec.json.Jackson2JsonEncoder) CodecConfigurer(org.springframework.http.codec.CodecConfigurer) Test(org.junit.jupiter.api.Test)

Example 23 with Jackson2JsonEncoder

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

the class CodecConfigurerTests method cloneDefaultCodecs.

@Test
void cloneDefaultCodecs() {
    CodecConfigurer clone = this.configurer.clone();
    Jackson2JsonDecoder jacksonDecoder = new Jackson2JsonDecoder();
    Jackson2JsonEncoder jacksonEncoder = new Jackson2JsonEncoder();
    Jaxb2XmlDecoder jaxb2Decoder = new Jaxb2XmlDecoder();
    Jaxb2XmlEncoder jaxb2Encoder = new Jaxb2XmlEncoder();
    ProtobufDecoder protoDecoder = new ProtobufDecoder();
    ProtobufEncoder protoEncoder = new ProtobufEncoder();
    clone.defaultCodecs().jackson2JsonDecoder(jacksonDecoder);
    clone.defaultCodecs().jackson2JsonEncoder(jacksonEncoder);
    clone.defaultCodecs().jaxb2Decoder(jaxb2Decoder);
    clone.defaultCodecs().jaxb2Encoder(jaxb2Encoder);
    clone.defaultCodecs().protobufDecoder(protoDecoder);
    clone.defaultCodecs().protobufEncoder(protoEncoder);
    // Clone has the customized the customizations
    List<Decoder<?>> decoders = clone.getReaders().stream().filter(reader -> reader instanceof DecoderHttpMessageReader).map(reader -> ((DecoderHttpMessageReader<?>) reader).getDecoder()).collect(Collectors.toList());
    List<Encoder<?>> encoders = clone.getWriters().stream().filter(writer -> writer instanceof EncoderHttpMessageWriter).map(reader -> ((EncoderHttpMessageWriter<?>) reader).getEncoder()).collect(Collectors.toList());
    assertThat(decoders).contains(jacksonDecoder, jaxb2Decoder, protoDecoder);
    assertThat(encoders).contains(jacksonEncoder, jaxb2Encoder, protoEncoder);
    // Original does not have the customizations
    decoders = this.configurer.getReaders().stream().filter(reader -> reader instanceof DecoderHttpMessageReader).map(reader -> ((DecoderHttpMessageReader<?>) reader).getDecoder()).collect(Collectors.toList());
    encoders = this.configurer.getWriters().stream().filter(writer -> writer instanceof EncoderHttpMessageWriter).map(reader -> ((EncoderHttpMessageWriter<?>) reader).getEncoder()).collect(Collectors.toList());
    assertThat(decoders).doesNotContain(jacksonDecoder, jaxb2Decoder, protoDecoder);
    assertThat(encoders).doesNotContain(jacksonEncoder, jaxb2Encoder, protoEncoder);
}
Also used : CharSequenceEncoder(org.springframework.core.codec.CharSequenceEncoder) Decoder(org.springframework.core.codec.Decoder) ByteBufferEncoder(org.springframework.core.codec.ByteBufferEncoder) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ResourceHttpMessageWriter(org.springframework.http.codec.ResourceHttpMessageWriter) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FormHttpMessageReader(org.springframework.http.codec.FormHttpMessageReader) HttpMessageReader(org.springframework.http.codec.HttpMessageReader) ServerSentEventHttpMessageReader(org.springframework.http.codec.ServerSentEventHttpMessageReader) Jackson2JsonEncoder(org.springframework.http.codec.json.Jackson2JsonEncoder) NettyByteBufDecoder(org.springframework.core.codec.NettyByteBufDecoder) KotlinSerializationJsonEncoder(org.springframework.http.codec.json.KotlinSerializationJsonEncoder) EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BDDMockito.given(org.mockito.BDDMockito.given) ServerSentEventHttpMessageWriter(org.springframework.http.codec.ServerSentEventHttpMessageWriter) DataBufferDecoder(org.springframework.core.codec.DataBufferDecoder) Jaxb2XmlDecoder(org.springframework.http.codec.xml.Jaxb2XmlDecoder) ResolvableType(org.springframework.core.ResolvableType) ResourceHttpMessageReader(org.springframework.http.codec.ResourceHttpMessageReader) Encoder(org.springframework.core.codec.Encoder) Jackson2SmileDecoder(org.springframework.http.codec.json.Jackson2SmileDecoder) ProtobufDecoder(org.springframework.http.codec.protobuf.ProtobufDecoder) Jackson2JsonDecoder(org.springframework.http.codec.json.Jackson2JsonDecoder) Jaxb2XmlEncoder(org.springframework.http.codec.xml.Jaxb2XmlEncoder) KotlinSerializationJsonDecoder(org.springframework.http.codec.json.KotlinSerializationJsonDecoder) StringDecoder(org.springframework.core.codec.StringDecoder) MediaType(org.springframework.http.MediaType) ByteArrayEncoder(org.springframework.core.codec.ByteArrayEncoder) NettyByteBufEncoder(org.springframework.core.codec.NettyByteBufEncoder) CodecConfigurer(org.springframework.http.codec.CodecConfigurer) MimeTypeUtils(org.springframework.util.MimeTypeUtils) ByteBufferDecoder(org.springframework.core.codec.ByteBufferDecoder) Collectors(java.util.stream.Collectors) ByteArrayDecoder(org.springframework.core.codec.ByteArrayDecoder) DecoderHttpMessageReader(org.springframework.http.codec.DecoderHttpMessageReader) Test(org.junit.jupiter.api.Test) DataBufferEncoder(org.springframework.core.codec.DataBufferEncoder) ProtobufEncoder(org.springframework.http.codec.protobuf.ProtobufEncoder) List(java.util.List) Jackson2SmileEncoder(org.springframework.http.codec.json.Jackson2SmileEncoder) ProtobufHttpMessageWriter(org.springframework.http.codec.protobuf.ProtobufHttpMessageWriter) ExtensionRegistry(com.google.protobuf.ExtensionRegistry) HttpMessageWriter(org.springframework.http.codec.HttpMessageWriter) Mockito.mock(org.mockito.Mockito.mock) DecoderHttpMessageReader(org.springframework.http.codec.DecoderHttpMessageReader) ProtobufEncoder(org.springframework.http.codec.protobuf.ProtobufEncoder) Jackson2JsonDecoder(org.springframework.http.codec.json.Jackson2JsonDecoder) Jaxb2XmlDecoder(org.springframework.http.codec.xml.Jaxb2XmlDecoder) ProtobufDecoder(org.springframework.http.codec.protobuf.ProtobufDecoder) Decoder(org.springframework.core.codec.Decoder) NettyByteBufDecoder(org.springframework.core.codec.NettyByteBufDecoder) DataBufferDecoder(org.springframework.core.codec.DataBufferDecoder) Jaxb2XmlDecoder(org.springframework.http.codec.xml.Jaxb2XmlDecoder) Jackson2SmileDecoder(org.springframework.http.codec.json.Jackson2SmileDecoder) ProtobufDecoder(org.springframework.http.codec.protobuf.ProtobufDecoder) Jackson2JsonDecoder(org.springframework.http.codec.json.Jackson2JsonDecoder) KotlinSerializationJsonDecoder(org.springframework.http.codec.json.KotlinSerializationJsonDecoder) StringDecoder(org.springframework.core.codec.StringDecoder) ByteBufferDecoder(org.springframework.core.codec.ByteBufferDecoder) ByteArrayDecoder(org.springframework.core.codec.ByteArrayDecoder) Jaxb2XmlEncoder(org.springframework.http.codec.xml.Jaxb2XmlEncoder) Jackson2JsonEncoder(org.springframework.http.codec.json.Jackson2JsonEncoder) EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) CharSequenceEncoder(org.springframework.core.codec.CharSequenceEncoder) ByteBufferEncoder(org.springframework.core.codec.ByteBufferEncoder) Jackson2JsonEncoder(org.springframework.http.codec.json.Jackson2JsonEncoder) KotlinSerializationJsonEncoder(org.springframework.http.codec.json.KotlinSerializationJsonEncoder) Encoder(org.springframework.core.codec.Encoder) Jaxb2XmlEncoder(org.springframework.http.codec.xml.Jaxb2XmlEncoder) ByteArrayEncoder(org.springframework.core.codec.ByteArrayEncoder) NettyByteBufEncoder(org.springframework.core.codec.NettyByteBufEncoder) DataBufferEncoder(org.springframework.core.codec.DataBufferEncoder) ProtobufEncoder(org.springframework.http.codec.protobuf.ProtobufEncoder) Jackson2SmileEncoder(org.springframework.http.codec.json.Jackson2SmileEncoder) CodecConfigurer(org.springframework.http.codec.CodecConfigurer) Test(org.junit.jupiter.api.Test)

Example 24 with Jackson2JsonEncoder

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

the class ServletOAuth2AuthorizedClientExchangeFilterFunctionTests method getBody.

private static String getBody(ClientRequest request) {
    final List<HttpMessageWriter<?>> messageWriters = new ArrayList<>();
    messageWriters.add(new EncoderHttpMessageWriter<>(new ByteBufferEncoder()));
    messageWriters.add(new EncoderHttpMessageWriter<>(CharSequenceEncoder.textPlainOnly()));
    messageWriters.add(new ResourceHttpMessageWriter());
    Jackson2JsonEncoder jsonEncoder = new Jackson2JsonEncoder();
    messageWriters.add(new EncoderHttpMessageWriter<>(jsonEncoder));
    messageWriters.add(new ServerSentEventHttpMessageWriter(jsonEncoder));
    messageWriters.add(new FormHttpMessageWriter());
    messageWriters.add(new EncoderHttpMessageWriter<>(CharSequenceEncoder.allMimeTypes()));
    messageWriters.add(new MultipartHttpMessageWriter(messageWriters));
    BodyInserter.Context context = new BodyInserter.Context() {

        @Override
        public List<HttpMessageWriter<?>> messageWriters() {
            return messageWriters;
        }

        @Override
        public Optional<ServerHttpRequest> serverRequest() {
            return Optional.empty();
        }

        @Override
        public Map<String, Object> hints() {
            return new HashMap<>();
        }
    };
    MockClientHttpRequest body = new MockClientHttpRequest(HttpMethod.GET, "/");
    request.body().insert(body, context).block();
    return body.getBodyAsString().block();
}
Also used : ByteBufferEncoder(org.springframework.core.codec.ByteBufferEncoder) Context(reactor.util.context.Context) OAuth2AuthorizationContext(org.springframework.security.oauth2.client.OAuth2AuthorizationContext) FormHttpMessageWriter(org.springframework.http.codec.FormHttpMessageWriter) HttpMessageWriter(org.springframework.http.codec.HttpMessageWriter) ServerSentEventHttpMessageWriter(org.springframework.http.codec.ServerSentEventHttpMessageWriter) MultipartHttpMessageWriter(org.springframework.http.codec.multipart.MultipartHttpMessageWriter) ResourceHttpMessageWriter(org.springframework.http.codec.ResourceHttpMessageWriter) EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) HashMap(java.util.HashMap) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) ArrayList(java.util.ArrayList) ServerSentEventHttpMessageWriter(org.springframework.http.codec.ServerSentEventHttpMessageWriter) MockClientHttpRequest(org.springframework.mock.http.client.reactive.MockClientHttpRequest) BodyInserter(org.springframework.web.reactive.function.BodyInserter) Jackson2JsonEncoder(org.springframework.http.codec.json.Jackson2JsonEncoder) ResourceHttpMessageWriter(org.springframework.http.codec.ResourceHttpMessageWriter) FormHttpMessageWriter(org.springframework.http.codec.FormHttpMessageWriter) MultipartHttpMessageWriter(org.springframework.http.codec.multipart.MultipartHttpMessageWriter)

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