Compilation error: No source code is available for type com.google.gson.Gson

Compilation error: No source code is available for type com.google.gson.Gson

我正在尝试使用 Gson 在客户端解析 json 字符串,但在安装项目时出现错误:

 No source code is available for type com.google.gson.Gson; did you forget to inherit a required module?

我在 gwt.xml 上添加了 <inherits name='com.google.gson.Gson' />。 我正在使用 GWT 2.8.1 我们可以在客户端使用 Gson 吗?
[这][1]

using Gson library in GWT client code 使用旧版本的 GWT,我的问题也与 Gson 模块的错误有关。

Gson 不兼容 GWT。您应该使用像 gwt-jackson 这样的 GWT 兼容库。

或者,如果您的模型很简单,您可以使用一种称为 JsInterop DTO 的技术。并直接使用浏览器原生的JSON.parse功能。此技术基于 JsInterop and some limitation explained here。示例:

@JsType(isNative = true, namespace = JsPackage.GLOBAL, name = "Object") 
class SearchResult {
    public String display_name; //ex: "Málaga, Provincia de Málaga, Andalusia, Spain"
    public String lat, lon; //ex: lat: "36.7210805", lon: "-4.4210409"
    public double importance; //ex: 0.73359836669253
}

然后调用native parse(需要导入elemental2-core库):

Object jsonObj = elemental2.core.Global.JSON.parse(jsonStr);
SearchResult result = jsinterop.base.Js.cast(jsonObj);