1
votes

I am wondering how I can use ember-data working with a Spring/Hibernate Java backend. I would usually use Jackson to return JSON but that does not appear support the specifications required by jsonapi.org.

Currently beans are returned in a hierarchical nature like this.

{
  "id" : 1,
  "name" : "Bill",
  "surname" : "Smith",
  "address" : {
    "id" : 23,
    "number" : 21,
    "street" : "Vincent st",
    "state" : {
       "id" : 44,
       "name" : "Victoria"
       "abbreviation" : "VIC"
    }
    "postcode" : 9000
  }
}

I am not stuck to this structure and it can be modified to suite jsonapi, however, I can't find any jackson plugin that will serialize/deserialize my objects to the jsonapi specification.

What are my options here? I know that I am able to write my own serializer/deserializer for ember-data but that would be a huge pain, surely there are other people using ember with a java backend.

2
This post should give you some hints: springember.blogspot.de/2014/08/… - Dirk Lachowski

2 Answers

1
votes

This looks familiar to an issue I had parsing json with DataFX (using javaFX), json was also generated by Spring... I ended up returning a Page which wraps your Person and creates some json that is parseable. Just my 2cts... I added the following requestmapping to my PersonController:

@RequestMapping(value="/persons", method=RequestMethod.GET, headers="Accept=application/json, application/xml")
public @ResponseBody Page<Person> getPersons(
    @RequestParam(value="page",required=false,defaultValue="0") String page,
    @RequestParam(value="size",required=false,defaultValue="20") String size,
    @RequestParam(value="orderby",required=false,defaultValue="name") String orderby){
        PersonRepository repository = context.getBean(PersonRepository.class);
        final PageRequest pr = new PageRequest( Integer.parseInt(page), Integer.parseInt(size), Direction.ASC, orderby);
        Page<Person> persons = (Page<Person>) repository.findAll(pr);
        return persons;
}
0
votes

I'm new to ember and still in the design stages with php as a backend, but I believe I have the same problem and found what looks like a fix. You're right that messing with jackson would be a difficult approach. It seems like it's much easier to make the change on the ember side. This guide (http://lab.empirio.no/emberjs-say-hello-to-apigility.html) discusses creating our own serializer in js based on ember data's ActiveModelSerializer and then modify the RestAdapter. The example is discussing building the standard ToDo app that I'm sure you've seen used as an example already.

The problem is the backend uses this format:

{"name":"Testing","is_completed":false}

While ember uses:

{"todo":{"name":"Testing","is_completed":false}}

Here's some same code:

App.ApplicationSerializer = DS.ActiveModelSerializer.extend({
    primaryKey: 'todos_id',

    extract: function(store, type, payload, id, requestType) {
        this.extractMeta(store, type, payload);

        if(payload._embedded)
            payload = payload._embedded;

        if(requestType == 'updateRecord' || requestType == 'createRecord'){
            var data = {};
            data[type.typeKey] = payload;
            payload = data;
        }
        var specificExtract = "extract" + requestType.charAt(0).toUpperCase() + requestType.substr(1);
        return this[specificExtract](store, type, payload, id, requestType);
    }
});

... "we manipulate the payload by extending (copying from the RESTAdapter). the createRecord-function changes the behavior on the adapter like this: "

 createRecord: function(store, type, record) {
        var data = {};
        var serializer = store.serializerFor(type.typeKey);
        serializer.serializeIntoHash(data, type, record, { includeId: true });
        return this.ajax(this.buildURL(type.typeKey), "POST", { data: data[type.typeKey] });
    },

I'm sure I'm missing a lot here and probably missing some steps since I haven't even tried to build the app yet, but this was a problem I knew I needed to address before I committed to ember and this seems like a viable solution. I hope its a step in the right direction for you anyway.

edit: I know you didn't want to change the format for the sake of sticking to the standard, but there really isn't a standard for JSON APIs and content types yet, not one that's accepted anyway. This guide uses HAL + JSON which doesn't look any different from what I see at jsonapi.org if that's what you were talking about. Regardless, everyone seems to be having this issue regardless of backend language or frameworks.I think the ember team recognizes this and are trying to be flexible. Also, since ember-data is still in beta, I'd be more apt to make the changes there instead of writing the java side to support a changing library. Who knows? Maybe ember-data will have better support for different backends before its 1.0 release, although I haven't heard any mention of it on the roadmap.