Browse Source

Ability to customize default Smile codecs

Closes gh-24382
pull/24418/head
Rossen Stoyanchev 5 years ago
parent
commit
5e9d29d813
  1. 18
      spring-web/src/main/java/org/springframework/http/codec/CodecConfigurer.java
  2. 40
      spring-web/src/main/java/org/springframework/http/codec/support/BaseDefaultCodecs.java
  3. 8
      spring-web/src/test/java/org/springframework/http/codec/support/CodecConfigurerTests.java

18
spring-web/src/main/java/org/springframework/http/codec/CodecConfigurer.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -123,6 +123,22 @@ public interface CodecConfigurer { @@ -123,6 +123,22 @@ public interface CodecConfigurer {
*/
void jackson2JsonEncoder(Encoder<?> encoder);
/**
* Override the default Jackson Smile {@code Decoder}.
* <p>Note that {@link #maxInMemorySize(int)}, if configured, will be
* applied to the given decoder.
* @param decoder the decoder instance to use
* @see org.springframework.http.codec.json.Jackson2SmileDecoder
*/
void jackson2SmileDecoder(Decoder<?> decoder);
/**
* Override the default Jackson Smile {@code Encoder}.
* @param encoder the encoder instance to use
* @see org.springframework.http.codec.json.Jackson2SmileEncoder
*/
void jackson2SmileEncoder(Encoder<?> encoder);
/**
* Override the default Protobuf {@code Decoder}.
* <p>Note that {@link #maxInMemorySize(int)}, if configured, will be

40
spring-web/src/main/java/org/springframework/http/codec/support/BaseDefaultCodecs.java

@ -95,6 +95,12 @@ class BaseDefaultCodecs implements CodecConfigurer.DefaultCodecs, CodecConfigure @@ -95,6 +95,12 @@ class BaseDefaultCodecs implements CodecConfigurer.DefaultCodecs, CodecConfigure
@Nullable
private Encoder<?> jackson2JsonEncoder;
@Nullable
private Encoder<?> jackson2SmileEncoder;
@Nullable
private Decoder<?> jackson2SmileDecoder;
@Nullable
private Decoder<?> protobufDecoder;
@ -125,6 +131,8 @@ class BaseDefaultCodecs implements CodecConfigurer.DefaultCodecs, CodecConfigure @@ -125,6 +131,8 @@ class BaseDefaultCodecs implements CodecConfigurer.DefaultCodecs, CodecConfigure
protected BaseDefaultCodecs(BaseDefaultCodecs other) {
this.jackson2JsonDecoder = other.jackson2JsonDecoder;
this.jackson2JsonEncoder = other.jackson2JsonEncoder;
this.jackson2SmileDecoder = other.jackson2SmileDecoder;
this.jackson2SmileEncoder = other.jackson2SmileEncoder;
this.protobufDecoder = other.protobufDecoder;
this.protobufEncoder = other.protobufEncoder;
this.jaxb2Decoder = other.jaxb2Decoder;
@ -149,6 +157,16 @@ class BaseDefaultCodecs implements CodecConfigurer.DefaultCodecs, CodecConfigure @@ -149,6 +157,16 @@ class BaseDefaultCodecs implements CodecConfigurer.DefaultCodecs, CodecConfigure
this.protobufDecoder = decoder;
}
@Override
public void jackson2SmileDecoder(Decoder<?> decoder) {
this.jackson2SmileDecoder = decoder;
}
@Override
public void jackson2SmileEncoder(Encoder<?> encoder) {
this.jackson2SmileEncoder = encoder;
}
@Override
public void protobufEncoder(Encoder<?> encoder) {
this.protobufEncoder = encoder;
@ -208,8 +226,8 @@ class BaseDefaultCodecs implements CodecConfigurer.DefaultCodecs, CodecConfigure @@ -208,8 +226,8 @@ class BaseDefaultCodecs implements CodecConfigurer.DefaultCodecs, CodecConfigure
addCodec(readers, new ResourceHttpMessageReader(new ResourceDecoder()));
addCodec(readers, new DecoderHttpMessageReader<>(StringDecoder.textPlainOnly()));
if (protobufPresent) {
Decoder<?> decoder = this.protobufDecoder != null ? this.protobufDecoder : new ProtobufDecoder();
addCodec(readers, new DecoderHttpMessageReader<>(decoder));
addCodec(readers, new DecoderHttpMessageReader<>(this.protobufDecoder != null ?
(ProtobufDecoder) this.protobufDecoder : new ProtobufDecoder()));
}
addCodec(readers, new FormHttpMessageReader());
@ -324,11 +342,12 @@ class BaseDefaultCodecs implements CodecConfigurer.DefaultCodecs, CodecConfigure @@ -324,11 +342,12 @@ class BaseDefaultCodecs implements CodecConfigurer.DefaultCodecs, CodecConfigure
addCodec(readers, new DecoderHttpMessageReader<>(getJackson2JsonDecoder()));
}
if (jackson2SmilePresent) {
addCodec(readers, new DecoderHttpMessageReader<>(new Jackson2SmileDecoder()));
addCodec(readers, new DecoderHttpMessageReader<>(this.jackson2SmileDecoder != null ?
(Jackson2SmileDecoder) this.jackson2SmileDecoder : new Jackson2SmileDecoder()));
}
if (jaxb2Present) {
Decoder<?> decoder = this.jaxb2Decoder != null ? this.jaxb2Decoder : new Jaxb2XmlDecoder();
addCodec(readers, new DecoderHttpMessageReader<>(decoder));
addCodec(readers, new DecoderHttpMessageReader<>(this.jaxb2Decoder != null ?
(Jaxb2XmlDecoder) this.jaxb2Decoder : new Jaxb2XmlDecoder()));
}
// client vs server..
@ -383,8 +402,8 @@ class BaseDefaultCodecs implements CodecConfigurer.DefaultCodecs, CodecConfigure @@ -383,8 +402,8 @@ class BaseDefaultCodecs implements CodecConfigurer.DefaultCodecs, CodecConfigure
writers.add(new ResourceHttpMessageWriter());
writers.add(new EncoderHttpMessageWriter<>(CharSequenceEncoder.textPlainOnly()));
if (protobufPresent) {
Encoder<?> encoder = this.protobufEncoder != null ? this.protobufEncoder : new ProtobufEncoder();
writers.add(new ProtobufHttpMessageWriter((Encoder) encoder));
writers.add(new ProtobufHttpMessageWriter(this.protobufEncoder != null ?
(ProtobufEncoder) this.protobufEncoder : new ProtobufEncoder()));
}
return writers;
}
@ -416,11 +435,12 @@ class BaseDefaultCodecs implements CodecConfigurer.DefaultCodecs, CodecConfigure @@ -416,11 +435,12 @@ class BaseDefaultCodecs implements CodecConfigurer.DefaultCodecs, CodecConfigure
writers.add(new EncoderHttpMessageWriter<>(getJackson2JsonEncoder()));
}
if (jackson2SmilePresent) {
writers.add(new EncoderHttpMessageWriter<>(new Jackson2SmileEncoder()));
writers.add(new EncoderHttpMessageWriter<>(this.jackson2SmileEncoder != null ?
(Jackson2SmileEncoder) this.jackson2SmileEncoder : new Jackson2SmileEncoder()));
}
if (jaxb2Present) {
Encoder<?> encoder = this.jaxb2Encoder != null ? this.jaxb2Encoder : new Jaxb2XmlEncoder();
writers.add(new EncoderHttpMessageWriter<>(encoder));
writers.add(new EncoderHttpMessageWriter<>(this.jaxb2Encoder != null ?
(Jaxb2XmlEncoder) this.jaxb2Encoder : new Jaxb2XmlEncoder()));
}
return writers;
}

8
spring-web/src/test/java/org/springframework/http/codec/support/CodecConfigurerTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -252,6 +252,8 @@ public class CodecConfigurerTests { @@ -252,6 +252,8 @@ public class CodecConfigurerTests {
public 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();
@ -259,15 +261,19 @@ public class CodecConfigurerTests { @@ -259,15 +261,19 @@ public class CodecConfigurerTests {
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);
}

Loading…
Cancel
Save