springfox icon indicating copy to clipboard operation
springfox copied to clipboard

GSON support needed

Open Ziemowit opened this issue 7 years ago • 3 comments

1. Preface

In project based on Spring Boot + Spring Rest we are using GSON as message converter. We would like to add Springfox with swagger-ui to our application as it gives amazing functionality.

2. Issue

And it works when Jackson is used (we have checked it), but when GSON then swagger-ui throws an error.

We think that the reason for that is when we call "/v2/api-docs" we receive 2 different responses based on type of JSON converter used:

a) Jackson

{"swagger":"2.0","info":{"description":"Api Documentation", ...

b) GSON

{"value":{"swagger":"2.0" ...

3. Hack solution We have found lets say the "hack solution" here: http://stackoverflow.com/questions/30219946/springfoxswagger2-does-not-work-with-gsonhttpmessageconverterconfig but if possible we would like to avoid this.

4. Final request Would it be possible to add support for GSON in your application?

Ziemowit avatar Dec 16 '16 09:12 Ziemowit

Yes it would be possible but I don't have the time to do it. Would love to have an external contribution.

dilipkrish avatar Dec 19 '16 12:12 dilipkrish

I solved this problem. Import package is important

import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import java.lang.reflect.Type;
import springfox.documentation.spring.web.json.Json;

public class SwaggerJsonSerialize implements JsonSerializer<Json> {

  @Override
  public JsonElement serialize(Json json, Type type, JsonSerializationContext context) {
    final JsonParser parser = new JsonParser();
    return parser.parse(json.value());
  }
}

add to GsonHttpMessageConverter

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.springframework.http.converter.json.GsonHttpMessageConverter;
import springfox.documentation.spring.web.json.Json;

public class CustomHttpMessageConverter {

  public GsonHttpMessageConverter buildGson() {
    final Gson gson = new GsonBuilder()
        .registerTypeAdapter(Json.class, new SwaggerJsonSerialize())
        .serializeNulls()
        .create();

    final GsonHttpMessageConverter converter = new GsonHttpMessageConverter();
    converter.setGson(gson);
    return converter;
  }

}

Import the SwaggerJsonSerialized added above

Register the HttpMessageConverter (GsonHttpMessageConverter).

cheese10yun avatar Feb 19 '19 07:02 cheese10yun

We are using .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES) with naming convention of java to JSON and vice-versa on GSON like ` private GsonHttpMessageConverter customGsonHttpMessageConverter() { Gson gson = new GsonBuilder().setPrettyPrinting() .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES) .setExclusionStrategies(new AnnotationExclusionStrategy()) .addSerializationExclusionStrategy(new AnnotationExclusionSerializationStrategy()) .addDeserializationExclusionStrategy(new AnnotationExclusionDeserializationStrategy()) .registerTypeAdapter(PipelineDeviceContainer.class, new PipelineDeviceContainerDeserializer()) .registerTypeAdapter(Date.class, new DateSerializer()) .registerTypeAdapter(Date.class, new DateDeserializer()) .registerTypeAdapter(Json.class, new SpringfoxJsonToGsonAdapter()) .serializeNulls() .create();

    GsonHttpMessageConverter gsonMessageConverter = new GsonHttpMessageConverter();
    gsonMessageConverter.setGson(gson);

    return gsonMessageConverter;
}

private static class SpringfoxJsonToGsonAdapter implements JsonSerializer<Json> { @Override public JsonElement serialize(Json json, Type type, JsonSerializationContext context) { return JsonParser.parseString(json.value()); } } ` but it seems like it always expect an FieldNamingPolicy.IDENTITY convention, hence the first page of swagger-ui/index.html works, but expanding the API throws an exception.

Any pointers or idea?

dheerajsah avatar Jul 12 '23 18:07 dheerajsah