3
votes

I currently run a server that serves data like this:

single entity

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

list of entities

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

However, Ember Data expects data to follow the JSON API spec, in the format of

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

or else it will return error:

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

I currently have a responseFactory which will build the response as a map with key being the ember model ("users"/"user") the entity/list and value being the list/entity itself

Is there a better/cleaner way?

2

2 Answers

3
votes

Please check this project on github: https://github.com/brunorg/ember-java

What you need is the @JsonRootName Jackson annotation. I believe that this is the cleaner way.

1
votes

In your case try to use class like so :

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);
   }
}

and produce so :

@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);
}

it allow produce in format that Ember can accept like this

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