mercredi 8 avril 2015

RequestBody POJO always empty

I want to store some data from an HTML form (working with AngularJS) into my database, using Spring. For this, I'm using the @RequestBody annotation with a POJO, but I can't make it work: my POJO is instancied, but it looks like the POJO attributes are not mapped with my form values (they are all null).


Controller :



@RequestMapping(value = "/createEntities", method = RequestMethod.POST)
@ResponseBody
public List<Entity> createEntities(@RequestBody final EntityList resource, @RequestParam final String kind) {
System.out.println("Creating entity for: " + kind);
Preconditions.checkNotNull(resource);
List<Entity> newEntities = new ArrayList<Entity>();
System.out.println("Entity test = " + resource.getTest()); // Prints "Entity test = null"
// Code below returns NullException
//System.out.println("Entity list nb = " + resource.getEntity().size());
if (resource.getEntities() != null && !resource.getEntities().isEmpty()) {
System.out.println("Entity list is OK");
for (EntityForm eForm : resource.getEntities()) {
if (eForm.getGrant() != null) {
Entity ent = new Entity();
if ("RTS".equals(kind)) {
ent.setDept(deptService.findByAbr(DeptEnum.RTS.name()));
} else {
ent.setDept(deptService.findByAbr(DeptEnum.RTB.name()));
}
ent.setGrant(eForm.getGrant());
ent.setCountry(eForm.getCountry());
ent.setName(eForm.getName());
ent = service.create(ent);
newEntities.add(ent);
}
}
}
return newEntities;
}


EntityList is the POJO for my form. This POJO contains a list of EntityForm (+ a string for test purpose), which is a DTO for my database entity Entity.


EntityList POJO :



public class EntityList implements Serializable {
private static final long serialVersionUID = 6335318686899794229L;
private List<EntityForm> entities;
private String test;

public EntityList() {
super();
}

public EntityList(List<EntityForm> entities, String test) {
super();
this.entities = entities;
this.test = test;
}

public List<EntityForm> getEntities() {
return entities;
}

public void setEntities(List<EntityForm> entities) {
this.entities = entities;
}

public String getTest() {
return test;
}

public void setTest(String test) {
this.test = test;
}
}


I thought the problem came from a bad mapping between my list of entities in my form and my List<EntityForm> in my POJO, that's why I added a simple String to my POJO.


AngularJS side


Service :



app.factory("Entities", function($resource) {
return $resource("api/auth/entities", null,
{
createEntities: {method:'POST', url: "api/auth/entities/createEntities", params: { kind: '@kind' }, isArray:true}
});
})


Controller :



$scope.EntForm = {};
$scope.EntForm.entities = [];
$scope.EntForm.test = "myTest";

/* ... */

$scope.saveEnt= function() {
console.log($scope.EntForm);
Entities.createEntities($scope.EntForm,{kind:"RTS"},function(res) {
var msg = 'Entities created...';
ngToast.create(msg);
$location.path("/entities");
});
}


In my firefox console, I see that $scope.EntForm is correctly set (I have all my entity objects with the fields set, as well as the test string defined in the controller).


Result


All this code will display :



Creating entity for: RTS
Entity test = null


What am I doing wrong ?


Aucun commentaire:

Enregistrer un commentaire