mardi 10 novembre 2015

Spring MVC CRUD Controller with Ember Data

A have an CRUD Controller in my Spring MVC API.

This is my create method:

    @Override
    @JsonView(ApiView.FormView.class)
    @RequestMapping(method = RequestMethod.POST, consumes = ApiMediaType.jsonContentType, produces = ApiMediaType.jsonContentType)
    public ApiResponse create(@RequestBody @Valid Entity entity, BindingResult result) {
        if (result.hasErrors()) return new ApiResponse(result.getAllErrors());
        service.add(entity);
        return new ApiResponse(entity);
    }

This works good.

If I am sending such data to the API, it works

   {
        "email":"user@company.com",
        "password":"secret!",
        "firstName":"John",
        "lastName":"Doe",
        "title":"Mr."
    }

The problem is that I am using Ember JS with Ember Data. My adapter is sending this data:

{
   "data":{
      "attributes":{
         "email":"user@company.com",
         "firstName":"John",
         "lastName":"Doe",
         "title":"Mr.",
      },
      "type":"users"
   }
}

This is not a @Valid User form, that my UserController is expecting in the create method declared below:

public ApiResponse create(@RequestBody @Valid User form, BindingResult result) {}

How can I convert the data that Ember is sending to a User object into my Spring Application ?

Thanks and sorry for my english.




Aucun commentaire:

Enregistrer un commentaire