如何使用Google Gson而不是默认使用的默认春季杰克逊?

时间:2018-11-05 13:15:36

标签: java spring-boot swagger swagger-ui

我正在一个项目中,我想使用Swagger对其进行文档化。是一个使用Spring Boot实施的项目,我们使用的库是“ com.google.code.gson”,而不是SpringBoot“ Jackson”的默认库,但是使用Gson Swagger无效。

问题在于退货方式。

Gson返回如下:

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

杰克逊返回如下:

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

有人知道我如何使Gson正常工作吗?

3 个答案:

答案 0 :(得分:3)

默认情况下,Spring Boot使用Jackson来序列化和反序列化REST API中的请求和响应对象。 如果要使用GSON而不是Jackson,则可以在pom.xml或build.gradle文件中添加Gson依赖关系,并在application.properties文件中指定一个属性,以告诉Spring Boot将Gson用作首选的json映射器。

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

这就是您要做的一切!

答案 1 :(得分:0)

Swagger构造了自己的Json类以与前端进行通信(请参见springfox.documentation.spring.web.json.Json),其定义如下:

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;
  }
}

我们可以看到它使用Jackson定义的注释@JsonRawValue来表示Jackson应该使用方法value()的返回值作为Json对象的序列化结果,但是,此注释被Gson识别,并且序列化的结果变为

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

代替正确的响应格式:

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

该解决方案是为您的Gson bean自定义TypeAdapterJsonSerializer

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());
        }
    }
}

答案 2 :(得分:0)

老问题,但这是我的解决方案,使用 Spring Boot + OpenAPI + Swagger + Gson:

配置组件:

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 适配器:

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);
        }
    }
}

我希望它对某人有用。