【问题标题】:How to use autobean for converting json to java class in GWT如何在 GWT 中使用 autobean 将 json 转换为 java 类
【发布时间】:2017-08-22 10:14:05
【问题描述】:

我在gwt 中有一个Person 类,并且我发送了一个Person 的实例,其中servlet 使用Gson 从服务器到客户端进行了转换。但在客户端似乎我不能使用Gson。从我在论坛上看到的内容看来,最好的方法是使用AutoBeansJson 再次转换为对象Person

但是在AutoBeans 我只能使用一个接口。如果有人能帮我写,我将不胜感激。

我从服务器获取的json 示例并想再次转换为Person 类:

{"name":"aaa","family":"fff","username":"uuu","age":20,"phones":[{"id":0,"phoneNumber": "0911111"}],"亲戚":[null]}

public class Person implements Serializable {
   private String name;
   private String family;
   private String username;
   private int age;
   private List<Phone> phones;
   private List<Person> relatives;

   public Person() {
   }

   public Person(String name, String family, String username, int age, List<Phone> phones, List<Person> relatives) {
      this.name = name;
      this.family = family;
      this.username = username;
      this.age = age;
      this.phones = phones;
      this.relatives = new ArrayList<Person>();
      this.relatives = relatives;
   }

   public void addPhone(Phone p) {
      phones.add(p);
   }

   public String getName() {
      return this.name;
   }

   public String getFamily() {
      return this.family;
   }

   public int getAge() {
      return this.age;
   }

   public String getUsername() {
      return this.username;
   }

   public List<Phone> getNumbers() {
      return this.phones;
   }

   public List<Person> getRelatives() {
      return this.relatives;
   }

   public String getAllNumbers() {
      return Phone.convertPhonesToText(phones);
   }

   public static Person findPerson(List<Person> personList, String username) {
      // .....
   }

   public static List<Person> convertTextToPersons(List<Person> personList, String personsText) {
      // .....
   }

   public String convertPersonsToText() {
      // ....
   }
}

【问题讨论】:

标签: java gwt json-deserialization autobean


【解决方案1】:

是的,正如 Tobika 所评论的,另一个答案表明 AutoBeans 需要一个接口。如果您在客户端和服务器端都使用 AutoBeans,并且您将所有模型都定义为接口,则 AutoBeans 会更好。

如果您想使用您的类模型,您可以使用与 AutoBeans 非常相似的 GWT Jackson,但它使用您的模型,将 json 绑定到您的模型(如其他服务器端库;jackson、gson 等): https://github.com/nmorel/gwt-jackson

public static interface PersonMapper extends ObjectMapper<Person> {}

@Override public void onModuleLoad() {
    PersonMapper mapper = GWT.create(PersonMapper.class);

    String json = mapper.write(new Person("John", "Doe"));
    GWT.log( json ); // > {"firstName":"John","lastName":"Doe"}

    Person person = mapper.read(json);
    GWT.log(person.getFirstName() + " " + person.getLastName());
}

或者,您可以将纯 GWT 与 JsInterop 一起使用。这有很多限制,但即使有这个限制,它也是一个不错的选择。如果您可以避免在 DTO 中继承,这是我最喜欢的选择。但这具有超轻量级的巨大优势(实际上零开销映射开销和零代码开销,因为它使用本机解析并且没有副本,直接访问解析的 json 对象)。限制:不能使用继承,“损坏的类型系统”(所有 X instanceof SomeDtoType 返回总是 true,因为所有 DTO 都是 Object 类型,这很有意义,因为我们实际上使用的是解析的 JSON),不能仅使用本机数组的集合(但感谢 java8流这应该不是问题,无论您想从 Stream.of(arr) 开始做什么,并且仅支持 Double 和 Boolean 盒装类型(不支持任何花哨的类型,如 Date 或 BigInteger,不支持 long/Long.. .).

@JsType(isNative=true, package=GLOBAL, name="Object") final class Person {
    // you can use getter/setter but as this class is final DTO adds no value
    public String firstName; public String lastName; public Phome[] numbers;
    // you can add some helper methods, don't forget to skip serialization!
    public final @JsOverlay @JsonIgnore List<Phone> getNumberList() {
        return Stream.of(numbers).collect(Collectors.toList());
    }
}

@JsType(isNative=true, package=GLOBAL, name="Object) final class Phone {
    public String number;
}

@JsMethod(namespace = "JSON") public static native <T> T parse(String text);

@Override public void onModuleLoad() {
    Person person = parse("{\"firstName\":\"John\",\"lastName\":\"Doe\"}");
    GWT.log(person.firstName + " " + person.lastName);
}

这些简单而有限的 DTO 与其说是一种类型,不如说是一种 DTO 方案。但是有一个很大的优势,这个 DTO 可以与大多数服务器端解析器一起使用。 Jackson 和 GSON 无需任何配置即可进行编码和解析。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-24
    • 1970-01-01
    • 2021-05-09
    • 1970-01-01
    相关资源
    最近更新 更多