lundi 28 mars 2016

How to glue together Jersey Jax-RS with Ember's DS.RESTADAPTER?

Being quite new to Ember framework I ran into a problem on how to adapt the Jersey/Grizzly REST API for Ember frontend. Ember documentation assumes JSON API follows the standard. It suggests to write REST adapter but even it this case of writing custom adapter it seems to apply strict rules: The JSON payload should be an object that contains the record inside a root property.

My application is almost identical to Official Java 8 Tutorial):

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.Collection;

@Path("/items")
public class ItemsRest {

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Collection<Item> findItems() {
        return new ItemsService().getItems();
    }
}

ItemsService uses com.fasterxml.jackson.databind public class ObjectMapper. Jersey uses Jackson to handle the JSON conversion automatically but the resulted JSON cannot be parsed by Ember.

The response it prints out is in this format:

[{"id":1, "name":"examle", "otherProperties":"value"}, {...etc...}]

I need to create REST API on backend and provide some Javascript framework to handle it with no troubles.

What is the smartest and most straightforward approach? How to write Ember's DS.RESTADAPTER so it would be able to handle my data? Should I modify backend with something like this library jsonapi-converter?

Should I ditch Ember in favor of AngularJS or other frameworks that is less strict in terms of json api format/structure?

PS. Ember v2.4.0




Aucun commentaire:

Enregistrer un commentaire