如何在 swagger 中使用 Google Gson 而不是 Spring 的默认

我在一个项目中工作,我想使用 Swagger 记录它。是一个用 Spring Boot 实现的项目,我们使用库“com.google.code.gson”而不是默认的 SpringBoot“Jackson”,但 Gson Swagger 不起作用。

问题在于返回模式。

Gson返回如下:

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

杰克逊返回如下:

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

有谁知道我怎样才能让 Gson 工作?


PIPIONE
浏览 405回答 3
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这就是你需要做的!

MMTTMM

老问题,但这是我的解决方案,使用 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;@Configurationpublic class GsonConfiguration {&nbsp; &nbsp; @Bean&nbsp; &nbsp; public Gson gson() {&nbsp; &nbsp; &nbsp; &nbsp; return new GsonBuilder()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .registerTypeAdapter(String.class, new SwaggerJsonSerializer())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .create();&nbsp; &nbsp; }}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> {&nbsp; &nbsp; @Override&nbsp; &nbsp; public JsonElement serialize(String json, Type typeOfSrc, JsonSerializationContext context) {&nbsp; &nbsp; &nbsp; &nbsp; if (json.contains("openapi")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return JsonParser.parseString(json.replace("\r", ""));&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return new Gson().toJsonTree(json, typeOfSrc);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}我希望它对某人有用。

互换的青春

Swagger 构建了自己的Json类来与前端进行通信(请参阅 参考资料springfox.documentation.spring.web.json.Json),其定义如下:import com.fasterxml.jackson.annotation.JsonRawValue;import com.fasterxml.jackson.annotation.JsonValue;public class Json {&nbsp; private final String value;&nbsp; public Json(String value) {&nbsp; &nbsp; this.value = value;&nbsp; }&nbsp; @JsonValue.&nbsp; &nbsp;// NOTICE THIS&nbsp; @JsonRawValue // NOTICE THIS&nbsp; public String value() {&nbsp; &nbsp; return value;&nbsp; }}我们可以看到它使用@JsonRawValueJackson定义的注解来表示Jackson应该使用方法的返回值value()作为Json对象的序列化结果,但是这个注解不被Gson识别,序列化结果变成{&nbsp; "value": "{\"swagger\":\"2.0\"...."}而不是正确的响应格式:{&nbsp; "swagger": "2.0",&nbsp; "info":[...],&nbsp; ...}解决方案是为您的 Gson bean定制一个TypeAdapter或JsonSerializerimport com.google.gson.*;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import springfox.documentation.spring.web.json.Json;@Configurationpublic class GsonConfig {&nbsp; &nbsp; @Bean&nbsp; &nbsp; public Gson gson() {&nbsp; &nbsp; &nbsp; &nbsp; return new GsonBuilder()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .registerTypeAdapter(Json.class, new SwaggerJsonTypeAdapter())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .create();&nbsp; &nbsp; }&nbsp; &nbsp; public static class SwaggerJsonTypeAdapter implements JsonSerializer<Json> {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public JsonElement serialize(Json json, Type type, JsonSerializationContext context) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return JsonParser.parseString(json.value());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java