在OSGI中忽略嵌套的内部类?

时间:2018-05-27 03:57:02

标签: java gson osgi classloader apache-karaf

我正在使用gson将一些json绑定到pojo。当我使用OSGI时,一切都完美绑定,所以我觉得由于某些类加载器问题,类类型被完全忽略,因为嵌套集合在解析后为null。

我有一个抽象的泛型类,它在一个单独的包中进行绑定:

  public <T> T deserialize(String jsonString, Class<T> clazz) {
    GsonBuilder builder = new GsonBuilder();
    Gson gson = builder.create();
    return gson.fromJson(jsonString, clazz);
  }

这种方法在没有OSGI的情况下工作,但是当我使用OSGI时,它只绑定T类中存在的顶级元素,而不绑定嵌套的内部类。

为了更好地说明“顶级”元素,titledescription被正确地反序列化到POJO中,但是Things为null。我是否需要以某种方式将嵌套的子类型嵌入到泛型中?

这是包含deserialize方法的类签名。

public abstract class MyAbstractClass<T>

{
  "title": "my awesome title",
  "description": "all the awesome things",
  "theThings": [
    {
      "thing": "coolThing1",
      "association-type": "thing"
    },
    {
      "thing": "coolThing2",
       "association-type": "thing"
      }

  ]
}

JSON绑定的POJO类:

package things;

import java.io.Serializable;
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class ThingsPOJO implements Serializable
{
  @SerializedName("title")
  @Expose
  public String title = "";

  @SerializedName("description")
  @Expose
  public String description = "";

  @SerializedName("theThings")
  @Expose
  public List<TheThing> theThings = null;

  private class TheThing implements Serializable
  {

    @SerializedName("thing")
    @Expose
    public String thing = "";

    @SerializedName("association-type")
    @Expose
    public String associationType = "";

  }
}

0 个答案:

没有答案
相关问题