From 5e9d29d8134b4be4733ae0a303d1c95b8c3d92c5 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Mon, 20 Jan 2020 17:29:59 +0000 Subject: [PATCH] Ability to customize default Smile codecs Closes gh-24382 --- .../http/codec/CodecConfigurer.java | 18 ++++++++- .../http/codec/support/BaseDefaultCodecs.java | 40 ++++++++++++++----- .../codec/support/CodecConfigurerTests.java | 8 +++- 3 files changed, 54 insertions(+), 12 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/http/codec/CodecConfigurer.java b/spring-web/src/main/java/org/springframework/http/codec/CodecConfigurer.java index 62d8a0eb3c..ec850a697c 100644 --- a/spring-web/src/main/java/org/springframework/http/codec/CodecConfigurer.java +++ b/spring-web/src/main/java/org/springframework/http/codec/CodecConfigurer.java @@ -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 { */ void jackson2JsonEncoder(Encoder encoder); + /** + * Override the default Jackson Smile {@code Decoder}. + *

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}. *

Note that {@link #maxInMemorySize(int)}, if configured, will be diff --git a/spring-web/src/main/java/org/springframework/http/codec/support/BaseDefaultCodecs.java b/spring-web/src/main/java/org/springframework/http/codec/support/BaseDefaultCodecs.java index 1862678388..64a2634f90 100644 --- a/spring-web/src/main/java/org/springframework/http/codec/support/BaseDefaultCodecs.java +++ b/spring-web/src/main/java/org/springframework/http/codec/support/BaseDefaultCodecs.java @@ -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 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 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 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 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 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 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; } diff --git a/spring-web/src/test/java/org/springframework/http/codec/support/CodecConfigurerTests.java b/spring-web/src/test/java/org/springframework/http/codec/support/CodecConfigurerTests.java index b2ca4667d5..71a1d7a848 100644 --- a/spring-web/src/test/java/org/springframework/http/codec/support/CodecConfigurerTests.java +++ b/spring-web/src/test/java/org/springframework/http/codec/support/CodecConfigurerTests.java @@ -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 { 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 { 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); }