diff options
author | Rashmi Pujar <rashmi.pujar1@bell.ca> | 2022-02-28 11:45:57 -0500 |
---|---|---|
committer | Rashmi Pujar <rashmi.pujar1@bell.ca> | 2022-02-28 11:45:57 -0500 |
commit | 11234c358bf65b7700c73d8bbad66b335b0dd3c6 (patch) | |
tree | d070b99c96938cec90e501b6e31ac65e1766d6bc /main/src | |
parent | b2e669ed9a55fea534e01262b04b4529fa2e998d (diff) |
cleaner way to handle swagger serialization issue
Instaed of altering the springboot default http
message converters, simply inject a GSON bean with
a custom type adapter with springfox Json serializer.
Issue-ID: POLICY-3753
Signed-off-by: Rashmi Pujar <rashmi.pujar1@bell.ca>
Change-Id: I0eb3b7cba538e7fd569f1cba9d540a7108db93e9
Diffstat (limited to 'main/src')
-rw-r--r-- | main/src/main/java/org/onap/policy/api/main/config/WebConfig.java | 23 |
1 files changed, 11 insertions, 12 deletions
diff --git a/main/src/main/java/org/onap/policy/api/main/config/WebConfig.java b/main/src/main/java/org/onap/policy/api/main/config/WebConfig.java index 88871f55..80477b2e 100644 --- a/main/src/main/java/org/onap/policy/api/main/config/WebConfig.java +++ b/main/src/main/java/org/onap/policy/api/main/config/WebConfig.java @@ -20,6 +20,7 @@ package org.onap.policy.api.main.config; +import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonParser; import com.google.gson.JsonSerializer; @@ -27,11 +28,11 @@ import java.util.Arrays; import java.util.List; import org.onap.policy.api.main.config.converter.StringToEnumConverter; import org.onap.policy.common.spring.utils.YamlHttpMessageConverter; +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.format.FormatterRegistry; import org.springframework.http.MediaType; import org.springframework.http.converter.HttpMessageConverter; -import org.springframework.http.converter.json.GsonHttpMessageConverter; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import springfox.documentation.spring.web.json.Json; @@ -50,22 +51,20 @@ public class WebConfig implements WebMvcConfigurer { var yamlConverter = new YamlHttpMessageConverter(); yamlConverter.setSupportedMediaTypes(Arrays.asList(MediaType.parseMediaType("application/yaml"))); converters.add(yamlConverter); - - GsonHttpMessageConverter converter = buildGsonConverter(); - converters.removeIf(GsonHttpMessageConverter.class::isInstance); - converters.add(0, converter); } /** * Swagger uses {{@link springfox.documentation.spring.web.json.Json}} which leads to Gson serialization errors. - * Hence, we customize a typeAdapter on the Gson bean in the Gson http message converter. + * Hence, we customize a typeAdapter on the Gson bean. * - * @return customised GSON HttpMessageConverter instance. + * @return customised GSON instance. */ - private GsonHttpMessageConverter buildGsonConverter() { - JsonSerializer<Json> serializer = (json, type, jsonSerializationContext) -> - JsonParser.parseString(json.value()); - var gson = new GsonBuilder().registerTypeAdapter(Json.class, serializer).create(); - return new GsonHttpMessageConverter(gson); + @Bean + public Gson gson() { + GsonBuilder gsonBuilder = new GsonBuilder(); + JsonSerializer<Json> serializer = + (json, type, jsonSerializationContext) -> JsonParser.parseString(json.value()); + gsonBuilder.registerTypeAdapter(Json.class, serializer); + return gsonBuilder.create(); } }
\ No newline at end of file |