1

我有一个类Persongwt我已经发送了一个使用从服务器到客户端Person转换的 servlet实例。Gson但在客户端似乎我无法使用Gson. 从我在论坛上读到的内容看来,最好的方法是使用再次AutoBeans转换Json为对象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() {
      // ....
   }
}
4

1 回答 1

1

是的,正如 Tobika 所评论的,另一个答案表明 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)) 开始做什么,

@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 无需任何配置即可进行编码和解析。

于 2017-08-23T18:16:34.553 回答