diff options
author | Jim Hahn <jrh3@att.com> | 2019-09-16 11:38:20 -0400 |
---|---|---|
committer | Jim Hahn <jrh3@att.com> | 2019-09-16 12:27:50 -0400 |
commit | 00ab9f38e08643acda3b560e01236ced0d77d5c2 (patch) | |
tree | ae1e560e9918be4de1dbbacad0e144f589ec761d /gson/src/main/java/org/onap/policy/common/gson/JacksonHandler.java | |
parent | af2e4018c92b0fb311f60dfe507335b12e2935a4 (diff) |
Eliminate constructors with side-effects
Several of the Gson and Jackson handlers take a GsonBuilder, which
they then configure with additional adapters prior to create a Gson
object that they subsequently use. The code has been modified so
that the constructors no longer take a GsonBuilder, thus eliminating
the side-effects that they had on the builders. Instead, a
configBuilder() method has been added to the handler classes that
makes it clear tht they modify the GsonBuilder.
This also has the advantage that classes that want a Gson object
configured per a given handler need only call that handler's
configBuilder() method; previously, they had to constructor the
handler and then retrieve its gson object.
Also updated a few tests to specify the HTTP "Accept" header to ensure
that return results are in yaml format.
Change-Id: I2ef98198041ff9f73913d01ee6ee14ecf20ba617
Issue-ID: POLICY-2081
Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'gson/src/main/java/org/onap/policy/common/gson/JacksonHandler.java')
-rw-r--r-- | gson/src/main/java/org/onap/policy/common/gson/JacksonHandler.java | 27 |
1 files changed, 19 insertions, 8 deletions
diff --git a/gson/src/main/java/org/onap/policy/common/gson/JacksonHandler.java b/gson/src/main/java/org/onap/policy/common/gson/JacksonHandler.java index 6d946439..6df5ad2e 100644 --- a/gson/src/main/java/org/onap/policy/common/gson/JacksonHandler.java +++ b/gson/src/main/java/org/onap/policy/common/gson/JacksonHandler.java @@ -20,6 +20,7 @@ package org.onap.policy.common.gson; +import com.google.gson.Gson; import com.google.gson.GsonBuilder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -36,20 +37,30 @@ public class JacksonHandler extends GsonMessageBodyHandler { * Constructs the object. */ public JacksonHandler() { - this(new GsonBuilder()); + this(configBuilder(new GsonBuilder()).create()); + } + /** + * Constructs the object. + * + * @param gson the Gson object to be used to serialize and de-serialize + */ + public JacksonHandler(Gson gson) { + super(gson); logger.info("Using GSON with Jackson behaviors for REST calls"); } /** - * Constructs the object. - * @param builder builder to use to create the gson object + * Configures a builder with the adapters normally used by this handler (e.g., + * adapters for GsonJsonXxx annotations). + * + * @param builder builder to be configured + * @return the configured builder */ - public JacksonHandler(GsonBuilder builder) { - super(builder - .registerTypeAdapterFactory(new JacksonFieldAdapterFactory()) + public static GsonBuilder configBuilder(GsonBuilder builder) { + return builder.registerTypeAdapterFactory(new JacksonFieldAdapterFactory()) .registerTypeAdapterFactory(new JacksonMethodAdapterFactory()) - .setExclusionStrategies(new JacksonExclusionStrategy())); + .registerTypeAdapterFactory(new MapDoubleAdapterFactory()) + .setExclusionStrategies(new JacksonExclusionStrategy()); } - } |