【问题标题】:Make Jersey RESTful server compliant with Ember Data and JSON API使 Jersey RESTful 服务器兼容 Ember Data 和 JSON API
【发布时间】:2013-07-12 05:23:11
【问题描述】:

我目前运行的服务器提供如下数据:

单一实体

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

实体列表

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

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

{"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 模型(“用户”/“用户”)实体/列表和值是列表/实体本身

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

【问题讨论】:

    标签: rest ember.js jersey ember-data json-api


    【解决方案1】:

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

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

    【讨论】:

    【解决方案2】:

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

    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...."
      } ]
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-15
      • 2013-01-14
      • 1970-01-01
      • 2011-02-17
      • 2018-01-30
      • 1970-01-01
      • 2019-06-20
      • 1970-01-01
      相关资源
      最近更新 更多