3

I'm working in a project and I would like to documentation it using Swagger. Is a project implemented with Spring Boot and we use the library "com.google.code.gson" instead of default of SpringBoot "Jackson", but with Gson Swagger not works.

The problem is with the pattern of return.

Gson return as follow:

{"value":"{\"swagger\":\"2.0\",\"info\":{\"description\":

Jackson return as follow:

{"swagger":"2.0","info":{"description"

Does anyone have any idea how can I make Gson works?

1
  • did you fixed this problem?@Eduardo Cintra I am facing the same problem.
    – Dolphin
    Jun 3, 2022 at 16:16

3 Answers 3

4

Spring Boot uses Jackson by default in order to serialize and deserialize request and response objects in the REST APIs. If you want to use GSON instead of Jackson then you can add Gson dependency in your pom.xml or build.gradle file and specify a property in the application.properties file to tell Spring Boot to use Gson as your preferred json mapper.

# Preferred JSON mapper to use for HTTP message conversion.
spring.http.converters.preferred-json-mapper=gson

that's all you need to do!

3
1

Swagger constructs it's own Json class to communicate with frontend (see springfox.documentation.spring.web.json.Json), which is defined as follow:

import com.fasterxml.jackson.annotation.JsonRawValue;
import com.fasterxml.jackson.annotation.JsonValue;

public class Json {
  private final String value;

  public Json(String value) {
    this.value = value;
  }

  @JsonValue.   // NOTICE THIS
  @JsonRawValue // NOTICE THIS
  public String value() {
    return value;
  }
}

We can see that it use annotation @JsonRawValue defined by Jackson to indicate that Jackson should use the return value of the method value() as serializing result of Json object, however, this annotation is not recognized by Gson, and serialized result becomes

{
  "value": "{\"swagger\":\"2.0\"...."
}

instead of the correct response format:

{
  "swagger": "2.0",
  "info":[...],
  ...
}

the solution is customize a TypeAdapter or JsonSerializer for your Gson bean

import com.google.gson.*;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import springfox.documentation.spring.web.json.Json;

@Configuration
public class GsonConfig {

    @Bean
    public Gson gson() {
        return new GsonBuilder()
                .registerTypeAdapter(Json.class, new SwaggerJsonTypeAdapter())
                .create();
    }

    public static class SwaggerJsonTypeAdapter implements JsonSerializer<Json> {

        @Override
        public JsonElement serialize(Json json, Type type, JsonSerializationContext context) {
            return JsonParser.parseString(json.value());
        }
    }
}

1

Old question but this is my solution, using Spring Boot + OpenAPI + Swagger + Gson:

Configuration Component:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.acepta.signerservices.core.gson.SwaggerJsonSerializer;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

@Configuration
public class GsonConfiguration {

    @Bean
    public Gson gson() {
        return new GsonBuilder()
                .registerTypeAdapter(String.class, new SwaggerJsonSerializer())
                .create();
    }
}

Gson Adapter:

import java.lang.reflect.Type;

import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

public class SwaggerJsonSerializer implements JsonSerializer<String> {

    @Override
    public JsonElement serialize(String json, Type typeOfSrc, JsonSerializationContext context) {
        if (json.contains("openapi")) {
            return JsonParser.parseString(json.replace("\r", ""));
        } else {
            return new Gson().toJsonTree(json, typeOfSrc);
        }
    }
}

I hope that it would be useful to someone.

1
  • it works for you ? I tried this way but seems did not work. @danoespinoza
    – Dolphin
    Jun 3, 2022 at 16:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.