3

我目前运行的服务器提供这样的数据:

单一实体

{"id":"11","name":"hello",....}

实体清单

[{single entity format},{},...]

但是,Ember Data 期望数据遵循JSON API 规范,格式为

{"entity":{"id":"11","name":"hello",....}} OR {"entities":[{},{},{}...]}

否则它将返回错误:

Your server returned a hash with the key 0 but you have no mapping for it

我目前有一个responseFactory将响应构建为映射,其中键是 ember 模型(“用户”/“用户”)实体/列表和值是列表/实体本身

有没有更好/更清洁的方法?

4

2 回答 2

3

请在 github 上查看这个项目:https ://github.com/brunorg/ember-java

您需要的是@JsonRootNameJackson 注释。我相信这是更清洁的方式。

于 2013-08-27T09:22:58.007 回答
1

在您的情况下,尝试像这样使用类:

import java.util.ArrayList;
import java.util.HashMap;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonRootName;

@JsonRootName("entity")
public class Entity {
   @JsonProperty
   public ArrayList<HashMap> entity = new ArrayList<HashMap>(); 

   public Entity() {    }

   public User(HashMap<String,Object> fields) {
      entity.add(fields);
   }
}

并产生:

@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Entity producePost(Object object) {
    HashMap<String, Object> f = new HashMap<String,Object>();
    f.put("id", "11");
    f.put("name", "hello...");
    return new Entity(f);
}

它允许以 Ember 可以接受的格式进行制作

 {
  "entity" : [ {
    "id" : "11",
    "name" : "hello...."
  } ]
}
于 2015-06-16T11:47:20.100 回答